获取“表的未知主键"当身份证在那里时 [英] Getting "Unknown primary key for table" while the ID is there
问题描述
我一直在调试 Rails 的这个奇怪问题,它给我表的主键未知...",即使表的 ID 存在也是如此.
I've been debugging this strange problem of Rails giving me "Unknown primary key for table...", even when the table's ID is there.
我已将数据库从一个 heroku 应用程序复制到另一个应用程序,在原始数据库上没有问题,而新数据库给了我一个 db 错误.
I've copied the database from one heroku app to another, on the original databse there is no problem and the new one gives me a db error.
这是错误:
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)
知道为什么会发生这种情况吗?
Any idea why this might happen?
谢谢!
推荐答案
似乎缺少表集合的主键.
Seems primary key is missing for the table collections.
在 Rails 3.2 之前,像这样在模型中设置主键
Prior to Rails 3.2, set the primary key in model like
class Collection < ActiveRecord::Base
set_primary_key "my_existing_column"
end
在 Rails 3.2+ 和 Rails 4 中,像这样设置模型中的主键
In Rails 3.2+ and Rails 4, set the primary key in model like
class Collection < ActiveRecord::Base
self.primary_key = "my_existing_column"
end
或
我们可以改变表并为id设置主键
We can alter the table and set the primary key for id like
创建迁移文件设置主键
class AddPrimaryKeyToCollections < ActiveRecord::Migration
def change
execute "ALTER TABLE collections ADD PRIMARY KEY (id);"
end
end
这篇关于获取“表的未知主键"当身份证在那里时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!