ruby-on-rails - 在 ID 存在时获取 "Unknown primary key for table"

标签 ruby-on-rails postgresql heroku rails-activerecord heroku-postgres

我一直在调试 Rails 的这个奇怪问题,它给我“表的未知主键...”,即使表的 ID 存在。

我已经将数据库从一个 heroku 应用程序复制到另一个,在原始数据库上没有问题,而新数据库给我一个数据库错误。

这是错误:

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          |                        Modifiers                         
----------------+------------------------+----------------------------------------------------------
 id             | integer                | not null default nextval('collections_id_seq'::regclass)
 name           | character varying(255) | 
 user_id        | integer                | 
 permalink      | character varying(255) | 
 category_id    | integer                | 
 products_count | integer                | 
 is_featured    | boolean                | 
Indexes:
    "index_lists_on_user_id_and_permalink" UNIQUE, btree (user_id, permalink)

知道为什么会发生这种情况吗?

谢谢!

最佳答案

似乎表集合缺少主键。

在 Rails 3.2 之前,像这样在模型中设置主键

class Collection < ActiveRecord::Base
  set_primary_key "my_existing_column"
end

在 Rails 3.2+ 和 Rails 4 中,像这样在模型中设置主键

class Collection < ActiveRecord::Base
  self.primary_key = "my_existing_column"
end

我们可以更改表并为id设置主键

创建迁移文件设置主键

class AddPrimaryKeyToCollections < ActiveRecord::Migration
 def change
   execute "ALTER TABLE collections ADD PRIMARY KEY (id);"
 end
end

关于ruby-on-rails - 在 ID 存在时获取 "Unknown primary key for table",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18056162/

相关文章:

ruby-on-rails - 如何判断 Rails 应用程序正在使用哪个版本的 gem

postgresql - 如何在 PostgreSQL 11.1 中将现有列更改为标识

postgresql - 在 plpgsgl 异常处理程序中获取违反的 fk 的名称

python - 在将 Django 应用程序部署到 Heroku 时收集静态错误

ruby-on-rails - 尝试在 ubuntu 16.04 上安装 ruby​​ gems 时权限被拒绝

ruby-on-rails - 从 rspec 读取 rails env 变量

mysql - sql语句的接受率

sql - 自己加入一个表 n 次

Heroku 运行显示更新,是否需要更新?如果是,那么如何?

java - 需要在 Java 中访问 JRuby on Rails 应用程序的 Heroku Postgres DB