获取“表的未知主键”而身份证在那里 [英] Getting "Unknown primary key for table" while the ID is there

查看:132
本文介绍了获取“表的未知主键”而身份证在那里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在调试Rails的这个trange问​​题,给我未知的表的主键...。已将数据库从一个heroku应用程序复制到另一个应用程序,原始数据库中没有问题,新数据库给我一个数据库错误。



这是错误:

  ProductsController#(ActionView :: Template :: Error)模型集合中表集合的未知主键。 

/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:在`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:在`each'中
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association_scope.rb :39:`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 /红宝石/ 2.0.0 /宝石/了activerecord-3.2.13 / LIB / active_rec ord / associations / association.rb:98:在`association_scope'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.13/lib/active_record/associations/association.rb:87 :在`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:在'last'
/app/app/helpers/likes_helper.rb:62:in'significant_liker'

导致它的行:

  product.collections.last.try:user 

和表格:

  d8apjspa441pad => \ d集合
表格public.collections
Column |类型|修饰符
---------------- + ------------------------ + --- -------------------------------------------------- -----
id |整数| not null default nextval('collections_id_seq':: regclass)
name |字符变化(255)|
user_id |整数|
permalink |字符变化(255)|
category_id |整数|
products_count |整数|
is_featured |布尔|
索引:
index_lists_on_user_id_and_permalinkUNIQUE,btree(user_id,permalink)

任何想法为什么会发生这种情况?



谢谢!

解决方案

似乎主键在表集合中缺失。



在Rails 3.2之前中,将模型中的主键设置为

  class集合< ActiveRecord :: Base 
set_primary_keymy_existing_column
结束

在Rails 3.2+和Rails 4 ,在模型中设置主键,如

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

OR



我们可以改变表格并为id设置主键,如

创建一个迁移文件来设置主键

  class AddPrimaryKeyToCollections< ActiveRecord :: Migration 
def change
执行ALTER TABLE collections ADD PRIMARY KEY(id);
结束
结束


I've been debugging this trange problem of Rails giving me "Unknown primary key for table...", even when the table's ID is there.

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.

This is the 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'

The line that causes it:

product.collections.last.try :user

and the table:

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?

Thanks!

解决方案

Seems primary key is missing for the table collections.

Prior to Rails 3.2, set the primary key in model like

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

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

OR

We can alter the table and set the primary key for id like

Create a migration file to set the primary key

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

这篇关于获取“表的未知主键”而身份证在那里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆