Why do I get an Unknown Primary Key exception for a join table in Rails 4?

后端 未结 5 521
情歌与酒
情歌与酒 2021-01-04 09:21

These are my models:

class Product
  has_many :line_items
  has_many :orders, :through => :line_items
end

class LineItem 
  belongs_to :order
  belongs_t         


        
相关标签:
5条回答
  • 2021-01-04 09:45

    In model add the following:

    self.primary_key = [:order_id, :product_id]
    

    and I think it would be wise to ensure that there's an index on those columns.

    You may create one with following migration:

    add_index :line_items, [:order_id, :product_id]
    
    0 讨论(0)
  • 2021-01-04 09:45

    I had this error, but dropping my local database with rake db:drop and then creating with rake db:create before running pg_restore with the heroku db dump solved it.

    0 讨论(0)
  • 2021-01-04 09:47

    The id: false in your original schema indicates that there is no id field in the table. Rails 4 added a create_join_table helper method which creates tables with no id field and uses this for any migration with JoinTable in the name.

    The only way I can imagine that you're getting difference results with Rails 4 than with Rails 3 is if you regenerated your migrations and had a migration with JoinTable in the name. Do you still have your schema from Rails 3 around? It would be interesting to note it has id: false for the join table.

    As for the primary_key, the reason you could set the primary key to an array but it subsequently didn't work is because the primary_key= method blindly converts its argument to a string per line 115 of https://github.com/rails/rails/blob/a0dfd84440f28d2862b7eb7ea340ca28d98fb23f/activerecord/lib/active_record/attribute_methods/primary_key.rb#L115

    See also https://stackoverflow.com/a/20016034/1008891 and its links.

    0 讨论(0)
  • 2021-01-04 09:57

    The accepted answer got rid of the error message, but I was still unable to save @order.line_items without getting an error telling me [:order_id, :product_id] does not exist.

    I finally solved this by deleting the line_items table and recreating it with this migration:

      def change
        create_table :line_items do |t|
          t.references :order
          t.references :product
          t.integer :quantity
          t.timestamps
        end
      end
    

    I hadn't used "references" when I created the table originally, which Rails 3 didn't mind, but made Rails 4 complain.

    0 讨论(0)
  • 2021-01-04 09:59

    Removing the id: false should fix your error. To do that, make a migration with the following line:

    add_column :model, :id, :primary_key

    0 讨论(0)
提交回复
热议问题