This project is archived and is in readonly mode.
HABTM Relationships Fail When Join Table has Primary Key
-
Jaime Bellmyer
- Tag changed from activerecord, duplicate, has_and_belongs_to_many, join_table, primary_key to activerecord, duplicate, has_and_belongs_to_many, join_table, patch, primary_key
After re-reading the ActiveRecord::Associations::ClassMethods docs, a new solution comes to mind. We're telling people in the docs not to have a primary key on the join table. Maybe instead of allowing primary keys to work error-free, we should raise an exception when the association is first made. This way having a primary key in your join table will fail right away, fail loudly, and with an error message that can be easily understood/researched.
If this is the better way to go, I can certainly implement it. Feedback is greatly appreciated.
-
Jaime Bellmyer
Can somebody with permissions please kill this ticket? I recently learned that this solution is not what the rails community wants. It's based on the legacy use of habtm before has_many :through came around. Now the use of habtm join tables is deprecated for anything fancy.
Still, we still have the problem of cryptic failure when the habtm join table contains a primary key. So I've created a different solution in ticket #2086 Primary key on HABTM join table now raises an exception that raises an exception with a clear explanation.
-
Pratik
- Assigned user set to Pratik
- State changed from new to invalid
- Title changed from [PATCH] HABTM Relationships Fail When Join Table has Primary Key to HABTM Relationships Fail When Join Table has Primary Key
-
Reto
Sorry for waking up this old ticket, but I almost shoot my foot due to this problem today.
In my opinion the documentation doesn't stress this problem enough, it says:
'The join table should not have a primary key or a model associated with it. You must manually generate the join table with a migration such as this:'[0]this even suggests that it is possible (technically), but one 'should not' use a primary key. But it is not possible and it reveals this ugly bug.
Would it possible to add a clear note about that problem in the documentation?
[0] http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassM...
