Tag: postgresql

当ID存在时,获取“表的未知主键”

我一直在调试这个Rails的奇怪问题,给我“桌子的未知主键……”,即使桌子的ID在那里。 我已经将数据库从一个heroku应用程序复制到另一个,在原始数据库上没有问题,新的一个给了我一个db错误。 这是错误: ProductsController# (ActionView::Template::Error) “Unknown primary key for table collections in model Collection.” /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/reflection.rb:366:in `primary_key’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/reflection.rb:480:in `association_primary_key’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:58:in `block in add_constraints’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:39:in `each’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:39:in `each_with_index’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:39:in `add_constraints’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb:31:in `scope’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:98:in `association_scope’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:87:in `scoped’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:573:in `first_or_last’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:105:in `last’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:46:in `last’ /app/app/helpers/likes_helper.rb:62:in `significant_liker’ 导致它的线: product.collections.last.try :user 和表: d8apjspa441pad=> \d collections Table “public.collections” Column | Type | […]

Rails:部署到Heroku,有很多问题

我第一次尝试将我的rails应用程序部署到Heroku,遇到了很多问题。 该应用程序崩溃,您可以查看图像中的日志: http : //i.stack.imgur.com/bsx1b.png 更糟糕的是,当我试图在本地查看我的应用程序时,它无法在postgreSQL环境中工作。当我回到在database.yml中使用sqLite3时工作正常。 我认为问题可能是我实际上必须安装postgreSQL,除了添加pg gem和运行bundle install。 我继续使用一键安装程序(Windows 7 64位)安装PostgreSQL。 重启后,一个简单的rails服务器或bundle install命令开始失败,所以我重新安装了整个ruby和rails。 现在rails server命令工作正常,当我尝试在本地环境中查看我的应用程序时,它会提示不同的错误。 所以现在我有pgAdmin III和Rails PG::Error fe_sendauth: no password supplied 我试着遵循这个 ,但我找不到“pg_hba.conf”。 我想答案是基于不同的操作系统。 现在再次,捆绑器不工作,给我错误…… 问题: 1)使用一键安装程序安装postgreSQL是否正确? 因为这导致我的ROR以某种方式“崩溃”,我不得不重新安装rails。 2)我想我将删除与postgreSQL相关的所有内容,重新安装Rails,并从头开始。 我必须采取哪些步骤? 我看过的所有参考文献似乎都不适合Windows环境。 在我的gemfile中,我有 gem ‘pg’ gem ‘thin’ 在我的数据库中,我有 # SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is […]

Rails范围 – 完全匹配的位置

是否可以在Rails中使用where IN (?)查询来查看范围,这将检查完全匹配? 例如: Post.joins(:tags).where(‘tags.id IN (?)’, [1, 2, 3, 4]) 会找到标签为1,2,1,2,3和1, 2post。 但是应该只找到带有1, 2, 3, 4标签的post。

如何在没有seg faulting的情况下使用Ruby 1.8.7在OS X Lion上运行“gem install”?

我是新手rails /终端用户,之后只做了一个新的Lion安装+ Xcode。 安装RVM但是当我在终端中执行“gem install [any gem]”时,我收到此错误: /Users/[my user]/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/timeout.rb:60: [BUG] Segmentation fault ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.0.0] Abort trap: 6 有人可以帮帮忙吗? PS。 我正在使用 – 在我正在进行的项目中,我必须使用ruby v1.8.7。

在不同的服务器上使用Rails的postgresql COPY命令的问题

我有一个已成功运行数月的rails应用程序。 在一些地方,我通过ActiveRecord :: Base.connection.execute(sql_code)直接调用数据库 由于最近需要扩展,我刚刚添加了第二台服务器进行数据处理。 我想运行相同的应用程序,但通过网络连接到其他数据库服务器。 这是唯一的区别。 应用程序的所有其他区域都可以工作 – 它可以连接到远程数据库。 它破坏的地方,是我有rails的地方发出psql COPY命令来导入csv文件。 result = ActiveRecord::Base.connection.execute( @PGSQL_COPY_COMMAND ) # perform the copy command 这失败并说无法找到csv文件。 我已经validation它在那里并且对于运行rails app和postgres用户的用户都是可读的。 我错过了什么吗?

按重复属性分组

基本上我有一个表messages , user_id字段标识创建消息的用户。 当我在两个用户之间显示一个对话(一组消息)时,我希望能够通过user_id对消息进行分组,但这是一种棘手的方式: 假设有一些消息(按created_at desc排序): id: 1, user_id: 1 id: 2, user_id: 1 id: 3, user_id: 2 id: 4, user_id: 2 id: 5, user_id: 1 我希望按以下顺序获得3个消息组: [1,2], [3,4], [5] 它应该按* user_id *分组,直到它看到另一个,然后按那个分组。 我正在使用PostgreSQL,并乐于使用特定的东西,无论什么能带来最佳性能。

推送到Heroku时Rails迁移错误w / Postgres

我正在尝试执行以下向上迁移以更改“tweet”模型表中的“number”列 class ChangeDataTypeForTweetsNumber < ActiveRecord::Migration def up change_column :tweets do |t| t.change :number, :integer end end def down change_table :tweets do |t| t.change :number, :string end end end 执行以下向上迁移到heroku …. heroku rake db:migrate:up VERSION=20120925211232 我收到以下错误 PG::Error: ERROR: column “number” cannot be cast to type integer : ALTER TABLE “tweets” ALTER COLUMN “number” TYPE integer 任何想法都会非常感激。 […]

MAC OS X上的Postgres权限被拒绝

我跟随ruby on rails教程运行postgres但是当我尝试“rake db:create”时,我收到以下错误: could not connect to server: Permission denied Is the server running locally and accepting connections on Unix domain socket “/var/pgsql_socket/.s.PGSQL.5432”? 我发现这个问题发出了一些类似的问题,但无论如何我都无法使它工作。 升级到OSX 10.7 Lion后修复Postgresql 我在阅读完这个问题后安装了自制软件,路径很好,brew医生告诉我路径上没有错误。

由于外键约束,无法删除对象

尝试做一个简单的user.destroy但user.destroy以下错误: 错误:表“user”上的更新或删除违反了表“identities”上的外键约束“fk_rails_5373344100”DETAIL:Key(id)=(2)仍然从表“identity”中引用。 这是我对身份的迁移 class CreateIdentities < ActiveRecord::Migration def change create_table :identities do |t| t.references :user, index: true, foreign_key: true t.string :provider t.string :uid t.timestamps null: false end end end 这是我的用户和身份模型: class Identity :provider def self.find_for_oauth(auth) find_or_create_by(uid: auth.uid, provider: auth.provider) end end 和用户: class User TEMP_EMAIL_REGEX, on: :update … end 我是外键和引用的新手,所以我不确定如何解决这个问题。 任何帮助将不胜感激。 谢谢

将pg_try_advisory_xact_lock()放在嵌套的子查询中?

在我的Ruby on Rails 4应用程序中,我对Postgres 9.4数据库进行了此查询: @chosen_opportunity = Opportunity.find_by_sql( ” UPDATE \”opportunities\” s SET opportunity_available = false FROM ( SELECT \”opportunities\”.* FROM \”opportunities\” WHERE ( deal_id = #{@deal.id} AND opportunity_available = true AND pg_try_advisory_xact_lock(id) ) LIMIT 1 FOR UPDATE ) sub WHERE s.id = sub.id RETURNING sub.prize_id, sub.id” ) 非常受dba.SE相关答案的启发 。 但是这里( Postgres pg_try_advisory_lock会阻止所有记录 )他们说,如果我没弄错的话,我不应该在WHERE子句中使用pg_try_advisory_lock() ,因为我会在扫描的整个集合中每行调用一次 […]