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

6 comments:

Ethan Vizitei said...

Thanks, you saved me a lot of time!

diablito said...

thanks a lot dude!

Unknown said...

Thank you! This really helped me out.

Anonymous said...

Triple everything everyone's already said! Thanks a bunch.

harold said...

Wow... I guess I missed this tidbit or forgot about it. I'm glad I googled it before spending too much time digging through my code. Thanks!

Shalmanese said...

Thanks! Saved me a lot of time