在那里获得ID“表的未知主键”

白宁

我一直在调试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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

注册表OpenSubkey返回null,尽管在那里

来自分类Dev

获得CS1513}是预期的,但是所有大括号都在那里

来自分类Dev

Cookie在那里,但我无法在CodeIgniter中获得它们。为什么?

来自分类Dev

Aapt失踪,但在那里

来自分类Dev

我收到FileNotFoundException但文件在那里

来自分类Dev

jQuery:缩小以居中并留在那里

来自分类Dev

杀死#### 但进程还在那里?

来自分类Dev

如何按表分组并创建新列以将原始值放在那里?

来自分类Dev

“找不到具有ID的列”,但该列在那里

来自分类Dev

无法通过 id 或标签找到片段....但它在那里

来自分类Dev

在Id和年度合并两个dataframes大熊猫在那里年是缺失值

来自分类Dev

SourceTree树视图在那里,但现在消失了

来自分类Dev

找不到类型或名称空间,但在那里

来自分类Dev

Android Dagger 2在那里时要求@Provides

来自分类Dev

为什么函数ls {ls; }挂在那里?

来自分类Dev

'ld:找不到用于建筑的符号???' 即使在那里

来自分类Dev

SSH:远程运行脚本并留在那里

来自分类Dev

即使UINavigation栏在那里,UIViewController的内容也不会显示

来自分类Dev

Devise无法检测到:omniauthable,但是它在那里

来自分类Dev

java NoClassDefFoundError但类在那里并且可用

来自分类Dev

无法从列表中删除项目,但显然在那里

来自分类Dev

输入没有数据错误,但数据在那里

来自分类Dev

为什么不在那里调用普通函数?

来自分类Dev

缺少所需的请求正文,但它在那里

来自分类Dev

.remove()之后元素还在那里吗?

来自分类Dev

Rails 4嵌套的资源/路线...几乎在那里...?

来自分类Dev

MSVCR100.dll丢失错误,即使在那里

来自分类Dev

ImportError:当模块在那里时,没有模块被命名

来自分类Dev

java NoClassDefFoundError但类在那里并且可用

Related 相关文章

  1. 1

    注册表OpenSubkey返回null,尽管在那里

  2. 2

    获得CS1513}是预期的,但是所有大括号都在那里

  3. 3

    Cookie在那里,但我无法在CodeIgniter中获得它们。为什么?

  4. 4

    Aapt失踪,但在那里

  5. 5

    我收到FileNotFoundException但文件在那里

  6. 6

    jQuery:缩小以居中并留在那里

  7. 7

    杀死#### 但进程还在那里?

  8. 8

    如何按表分组并创建新列以将原始值放在那里?

  9. 9

    “找不到具有ID的列”,但该列在那里

  10. 10

    无法通过 id 或标签找到片段....但它在那里

  11. 11

    在Id和年度合并两个dataframes大熊猫在那里年是缺失值

  12. 12

    SourceTree树视图在那里,但现在消失了

  13. 13

    找不到类型或名称空间,但在那里

  14. 14

    Android Dagger 2在那里时要求@Provides

  15. 15

    为什么函数ls {ls; }挂在那里?

  16. 16

    'ld:找不到用于建筑的符号???' 即使在那里

  17. 17

    SSH:远程运行脚本并留在那里

  18. 18

    即使UINavigation栏在那里,UIViewController的内容也不会显示

  19. 19

    Devise无法检测到:omniauthable,但是它在那里

  20. 20

    java NoClassDefFoundError但类在那里并且可用

  21. 21

    无法从列表中删除项目,但显然在那里

  22. 22

    输入没有数据错误,但数据在那里

  23. 23

    为什么不在那里调用普通函数?

  24. 24

    缺少所需的请求正文,但它在那里

  25. 25

    .remove()之后元素还在那里吗?

  26. 26

    Rails 4嵌套的资源/路线...几乎在那里...?

  27. 27

    MSVCR100.dll丢失错误,即使在那里

  28. 28

    ImportError:当模块在那里时,没有模块被命名

  29. 29

    java NoClassDefFoundError但类在那里并且可用

热门标签

归档