hak*_*nin 30 postgresql ruby-on-rails heroku rails-activerecord heroku-postgres
我一直在调试这个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'
Run Code Online (Sandbox Code Playgroud)
引起它的那条线:
product.collections.last.try :user
Run Code Online (Sandbox Code Playgroud)
和表:
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)
Run Code Online (Sandbox Code Playgroud)
知道为什么会这样吗?
谢谢!
Deb*_*att 37
表集合似乎缺少主键.
在Rails 3.2之前,在模型中设置主键
class Collection < ActiveRecord::Base
set_primary_key "my_existing_column"
end
Run Code Online (Sandbox Code Playgroud)
在Rails 3.2+和Rails 4中,在模型中设置主键
class Collection < ActiveRecord::Base
self.primary_key = "my_existing_column"
end
Run Code Online (Sandbox Code Playgroud)
要么
我们可以改变表并为id设置主键
创建迁移文件以设置主键
class AddPrimaryKeyToCollections < ActiveRecord::Migration
def change
execute "ALTER TABLE collections ADD PRIMARY KEY (id);"
end
end
Run Code Online (Sandbox Code Playgroud)
elc*_*elc 15
我遇到了类似的问题,这是我能找到的唯一一个页面.所以,以防万一对任何人都有帮助......
我突然开始在几张桌子上丢失主要密钥消息.我猜,但不确定,这是在推送数据后开始发生的(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的文档中的这个页面解释了:
运行迁移后,您将需要使用heroku restart重新启动应用程序以重新加载架构并获取任何架构更改.