Tag: postgresql

可以在Rails / ActiveRecord中指定允许带NULL的唯一索引吗?

我想在列上指定唯一索引,但我还需要允许NULL值(多个记录可以具有NULL值)。 在使用PostgreSQL进行测试时,我发现我可以拥有一个带有NULL值的记录,但下一个会导致一个问题: irb(main):001:0> u=User.find(5) User Load (111.1ms) SELECT “users”.* FROM “users” WHERE “users”.”id” = $1 LIMIT 1 [[“id”, 5]] => # irb(main):002:0> u.email=nil => nil irb(main):003:0> u.save (1.1ms) BEGIN User Exists (4.8ms) SELECT 1 AS one FROM “users” WHERE (“users”.”email” IS NULL AND “users”.”id” != 5) LIMIT 1 (1.5ms) ROLLBACK => false 因此,即使数据库允许,Rails也会首先检查User存在不同的ID,并将email列设置为NULL 。 有没有一种方法,不仅数据库可以允许它,但Rails也不会像上面那样先检查? […]

Rails 4:ActiveRecord不保存任何属性,保存默认值

我有一个带有一些属性的User模型,称它们为foo和bar 。 所以我的模型看起来像这样: class User < ActiveRecord::Base attr_accessor :foo, :bar end 然后我做以下事情: user = User.new user.foo = “123” user.save! 我的开发日志显示: INSERT INTO “users” DEFAULT VALUES RETURNING “id” 然后,如果我进入Rails控制台并执行User.first我得到类似的东西: # 我正在使用Postgres,我没有将其他模型保存到数据库,为什么我的User模型保存默认值?

ActiveRecord查询比直接SQL慢得多?

我一直在努力优化项目的数据库调用,我注意到下面两个相同调用之间的性能存在“显着”差异: connection = ActiveRecord::Base.connection() pgresult = connection.execute( “SELECT SUM(my_column) FROM table WHERE id = #{id} AND created_at BETWEEN ‘#{lower}’ and ‘#{upper}'”) 和第二个版本: sum = Table. where(:id => id, :created_at => lower..upper). sum(:my_column) 使用第一个版本的方法平均需要300毫秒才能执行(该操作在其中总共需要几千次),而使用第二个版本的方法需要大约550毫秒。 这几乎是速度下降100%。 我仔细检查了第二个版本生成的SQL,它与第一个版本的第一个相同,但是它使用表名添加表列。 为什么减速? ActiveRecord和SQL之间的转换是否真的使操作几乎耗费了2倍? 如果我需要执行相同的操作很多次并且我不想达到开销,我是否需要坚持直接编写SQL(甚至可能是sproc)? 谢谢!

Rails PG :: UndefinedTable:错误:缺少表的FROM子句条目

我有模特 class Offer < ActiveRecord::Base belongs_to :agency end class Agency < ActiveRecord::Base has_many :offers end 当我提出这样的要求时 – 一切都很好 @offers = Offer.with_state(:confirmed). includes(:destination, :cruise_line, :ship). paginate(per_page: 10, page: params[:page]).decorate 但我想只选择属于active代理商的商品( agencies表中的列state ),所以我尝试这样做: @offers = Offer.with_state(:confirmed). includes(:destination, :cruise_line, :ship). joins(:agency). where(agency: {state: ‘active’}). paginate(per_page: 10, page: params[:page]).decorate 完成此操作后,我收到错误PG::UndefinedTable: ERROR: missing FROM-clause entry for table “agency” 。 我的代码出了什么问题? […]

Postgres无权创建用户

我无法使用带有postgres的rails。 数据库不会创建: sayth@sayth-TravelMate-5740G:~/testapp2$ rake db:create:all PG::InsufficientPrivilege: ERROR: permission denied to create database : CREATE DATABASE “testapp2_development” ENCODING = ‘unicode’ …. Couldn’t create database for {“adapter”=>”postgresql”, “encoding”=>”unicode”, “database”=>”testapp2_production”, “pool”=>5, “username”=>”testapp2”, “password”=>nil} 所以我在这里关注解决方案https://stackoverflow.com/a/8639649 这些是我遇到的问题。 $ psql -d postgres postgres=# create role app_name login createdb; postgres=# \q 但是,当我在psql shell中为createb赋予create role时,它会失败。 sayth@sayth-TravelMate-5740G:~/testapp2$ psql -d postgres psql (9.2.4) Type “help” […]

PostgreSQL字符串(255)限制 – Rails,Ruby和Heroku

所以我有一个comments表,结构如下: # == Schema Information # # Table name: comments # # id :integer not null, primary key # body :string(255) # notified :boolean # user_id :integer # stage_id :integer # created_at :datetime # updated_at :datetime # client_id :integer # author :string(255) 这是我收到的错误消息: ActiveRecord::StatementInvalid (PGError: ERROR: value too long for type character varying(255) 如何使用Rails 3.x和Heroku将长文本存储在PG列中? […]

Heroku上的Rails 3.1中的Postgres重音不敏感LIKE搜索

如何在Rails中修改搜索查询的where / like条件: find(:all, :conditions => [“lower(name) LIKE ?”, “%#{search.downcase}%”]) 无论重音如何,结果都匹配? (例如métro= metro)。 因为我使用的是utf8,所以我不能使用“to_ascii”。 生产正在Heroku上运行。

为什么rails 5在模式文件中添加nextval方法?

升级到Rails 5后,我的架构文件在运行db:migrate时不断被更改。 Rails正在发生变化: create_table “flightlessons”, force: :cascade do |t| 至: create_table “flightlessons”, id: :integer, default: -> { “nextval(‘lessons_id_seq’::regclass)” }, force: :cascade do |t| 它只发生在这一个模型上。 为什么rails在这个特定型号上实现nextval? 而且,为什么它的模型名称错误(lessons_id_seq应该是flightlessons_id_seq)。 但是,手动将其更改为flightlessons_id_seq会导致相同的无关系错误。 PG::UndefinedTable: ERROR: relation “lessons_id_seq” does not exist 为了继续,我只需将schema.rb文件更改回该行应该是什么。 然后,我可以迁移或测试:准备或其他什么,直到下一次rails将其改回使用nextval方法。 感谢您对此的任何见解。

查看heroku上的数据库

我试图看看我的heroku实例上的数据。 我希望能够在他们的服务器上查看数据库.. 我不想把它拉到我的本地系统..更重要的是我不想等那么久,看看数据。 让我们假设我将当前数据库中的800万本书(以及所有相关的元数据)推送到heroku。 然后在两个月内我又增加了1200万。 (我仍处于开发模式)如果我对一批数据有问题,我认为下拉整个数据库是不可行的。 我想在等待下载数据时我可以做仰卧起坐。 我听说这是旧程序员在编译时所做的事情。 我看过Heroku中的Viewing数据库 ,它只是一个“不能这样做”。 真的吗? 我们无法查看实时数据?

我如何设置heroku postgresql应用程序的路径?

我刚刚安装了heroku PSQL应用程序(v1.0),我无法使用gem pg“0.1.4”连接我的rails应用程序。 我已经将路径PATH =“/ Applications / Postgres.app / Contents / MacOS / bin:$ PATH”添加到我的.profile和我的.bashrc文件中,但似乎没有任何东西允许我只是通过调用“psql”来运行psql。 我成功使用“psql -h localhost”。 当我选择“psql”时,我得到: Is the server running locally and accepting connections on Unix domain socket “/var/pgsql_socket/.s.PGSQL.5432”? 我正在使用山狮。