You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that running auto_upgrade! can create problems for has_and_belongs_to_many associations. In particular, if you have a habtm association between groups and users, but then call ActiveRecord::Base.auto_upgrade!, this will change the groups_users table to have the columns "group_id" and "left_side_id" (which should be "user_id"). This makes the User#groups method fail.
I figured that ActiveRecord::Base.auto_upgrade! would update all tables. But it seems like this way won't work. So is there an easier way to update all tables, other than listing them manually and then calling auto_upgrade! on them? (If you do ActiveRecord::Base.descendants.each(&:auto_upgrade!) it creates the same error. I suppose this is because it was trying to update the association table itself?) This way of doing it seems fragile and prone to error (say, if you add a new table and forget to manually add it to the list).
A related question is whether there is a way to know which tables have "migrations pending", i.e. which haven't been auto_upgrade'd to a schema matching the model source code.
The text was updated successfully, but these errors were encountered:
ibrahimtencer
changed the title
has_and_belongs_to_many does not work
auto_upgrade! sometimes breaks has_and_belongs_to_many associations
Jan 7, 2016
It seems that running auto_upgrade! can create problems for has_and_belongs_to_many associations. In particular, if you have a habtm association between groups and users, but then call ActiveRecord::Base.auto_upgrade!, this will change the groups_users table to have the columns "group_id" and "left_side_id" (which should be "user_id"). This makes the User#groups method fail.
I figured that ActiveRecord::Base.auto_upgrade! would update all tables. But it seems like this way won't work. So is there an easier way to update all tables, other than listing them manually and then calling auto_upgrade! on them? (If you do ActiveRecord::Base.descendants.each(&:auto_upgrade!) it creates the same error. I suppose this is because it was trying to update the association table itself?) This way of doing it seems fragile and prone to error (say, if you add a new table and forget to manually add it to the list).
A related question is whether there is a way to know which tables have "migrations pending", i.e. which haven't been auto_upgrade'd to a schema matching the model source code.
The text was updated successfully, but these errors were encountered: