ruby-on-rails – 为什么我在Rails 4中获得连接表的未知主键例外
发布时间:2020-12-16 19:01:38 所属栏目:百科 来源:网络整理
导读:这些是我的模特: class Product has_many :line_items has_many :orders,:through = :line_itemsendclass LineItem belongs_to :order belongs_to :productendclass Order has_many :line_items has_many :products,:through = :line_itemsend 来自schema.r
这些是我的模特:
class Product has_many :line_items has_many :orders,:through => :line_items end class LineItem belongs_to :order belongs_to :product end class Order has_many :line_items has_many :products,:through => :line_items end 来自schema.rb: create_table "line_items",id: false,force: true do |t| t.integer "order_id" t.integer "product_id" t.integer "quantity" t.datetime "created_at" t.datetime "updated_at" end 我刚刚升级到Rails 4,我的连接表停止工作.如果我执行@ order.line_items,它会抛出异常“模型LineItem中的表line_items的未知主键”. @ order.products按预期工作. 我已经尝试删除并重新创建line_items表,我已经尝试安装protected_attributes gem,但没有任何改变. 这是the trace. 解决方法
在模型添加
self.primary_key = [:order_id,:product_id] 我认为确保这些列上有索引是明智的.您可以使用以下迁移创建一个 add_index :line_items,[:order_id,:product_id] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |