Showing posts with label habtm. Show all posts
Showing posts with label habtm. Show all posts

Sunday, December 16, 2007

has_and_belongs_to_many (HABTM) and MySQL Duplicate entry error

A quick post:
If you use migrations to create a join table for a HABTM relationship, you should tell rails not to automatically add an id column in the migration (Agile book - 271).
Otherwise you will get a
Mysql::Error: Duplicate entry '#' for key #
error.


create_table :authors_books, :id => false do |t|
t.column :author_id, :integer, :null => false
t.column :book_id, :integer, :null => false
end