HABTM and Migrations Gotcha
If you are using migrations in your Rails apps (and you should) and you are also using has-and-belongs-to-many relationships then there is a gotcha to be aware of. The join tables in a HABTM relationship should *not* include an ID column, however migrations, by default, creates ID columns from all of your tables. Therefore, to avoid this problem, all join tables in HABTM relationships should include :id => false in the create_table invocation.
Example:
Example:
create_table :people_pets, :id => false do |t|Good stuff to know.
t.column :person_id, :integer, :null => false
t.column :pet_id, :integer, :null => false
end
1 Comments:
*Very* good stuff to know.
I couldn't figure out why the database table primary id kept mirroring that of the joined M:N table id.
Thanks for the tip. Saved me quite a bit of grief.
ciao !
Daryl.
By Anonymous, at 7:14 AM
Post a Comment
<< Home