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 #

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


Ethan Vizitei said...

Thanks, you saved me a lot of time!

Alejandro said...

thanks a lot dude!

Jim said...

Thank you! This really helped me out.

Abid 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!

Xianhang Zhang said...

Thanks! Saved me a lot of time

Anonymous said...

Thank you!