当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 | 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 

我遇到了类似的问题,这是我能找到的唯一一个页面。 所以万一它会对其他人有所帮助……

我突然开始在几张桌子上丢失主要密钥消息。 我猜,但不确定,这是在推送数据后开始发生的(pg_dump local, heroku pg:restore

有问题的主要密钥都是在已重命名的表上,以便pkey名称与表名不匹配 – 但另一方面,许多其他重命名的表都在同一条船上并且没有问题。

无论如何,在我挥之不去的一点上,我尝试上传另一个转储文件,我注意到有关违规索引的一些投诉。 首先,它会尝试删除它们并抱怨它不能,因为它们不存在。 后来它会尝试创建它们并抱怨它不能,因为它们已经存在。

非常烦人,因为pkey信息没有出现在schema.rb并且应该“正常工作”,对吧?

无论如何,对我有用的东西(以及我发布的原因)是做一个heroku pg:reset然后再次加载转储。 旁注,前两次尝试heroku pg:reset出现“内部服务器错误”。 但后来我再次尝试,它的确有效。

我最近遇到了这样的错误:“表的未知主键”,并且像问题提问者一样,它在将数据库复制到Heroku应用程序后出现。

源数据库没有错误,所以我确信表和主键都没问题。

我在这个页面上尝试了一些技巧,包括从头开始使用heroku pg:reset ,旧数据库的新pg_dump ,以及pgbackups:restore到新数据库,然后运行迁移和种子……没有用。

最终解决了我的问题很简单:重新启动应用程序。 新应用程序有许多数据库迁移,运行heroku restart加载模式并获取模式更改。 Heroku的文档中的这个页面解释了:

运行Rake命令

运行迁移后,您将需要使用heroku restart重新启动应用程序以重新加载架构并获取任何架构更改。

对我有帮助(在数据库恢复后发生在heroku上)是重新索引主键索引:

reindex index $primary_key_index_name

我正在从heroku恢复数据库转储到我的本地系统,并收到此错误..

 ActiveRecord::UnknownPrimaryKey: ActiveRecord::UnknownPrimaryKey 

我正在恢复现有数据库,所以我删除了数据库,创建了新数据库,然后恢复转储,它对我有用

重新启动heroku服务器为我工作。 在db-restorement期间,也许是Spring-preloader正在识别空的db-schema

如果您正在尝试解决此问题,请务必仔细检查您的日志。 我注意到与js资产相关的早期错误未被预编译。 这在渲染消息堆栈中丢失了。

一旦我修复了资产预编译问题,就不再抛出“表的未知主键”错误。

这绝对是我唯一改变的100%。

我遇到了这个问题,结果问题是我的表实际上没有主键索引。 解决方案是创建添加主键的迁移:

 execute "ALTER TABLE appointment_reminder_text ADD PRIMARY KEY (id)" 

谢谢改变上面的索引为我工作。 如果涉及更复杂的关系,只需另一个关于此错误将如何表现的快速说明:

 ActiveRecord::StatementInvalid - PG::SyntaxError: ERROR: zero-length delimited identifier at or near """" LINE 1: ...CT "users".* FROM "users" WHERE "benefits"."" IN ('1'...