Opened 15 years ago
Closed 15 years ago
#12386 closed (fixed)
Automatic ManyToMany Fields in models defined in packages use invalid column names
Reported by: | ldevesine | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | ||
Cc: | bnomis@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Using an M2M field in a model that is defined in a models package (that is, in member.models.membership where member is an app and app_label is defined as 'member' in the models) tries to access invalid column names to retrieve data.
In member.models.membership I have defined a MembershipType with a ManyToManyField subscriptions on SubscriptionType, defined in the same file. When I attempt to access this M2M relationship, I get the error:
"Unknown column 'membership_type_subscriptions.member.subscriptiontype_id' in 'field list'"
This bug appears only in the SVN version.
Attachments (3)
Change History (10)
comment:1 by , 15 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|
comment:2 by , 15 years ago
comment:3 by , 15 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
I can't reproduce this. FInd attached an hypothetical model_package_regress
regression test that shows Django is generation correct table and column names for the m2m field (tested with sqlite3 here) between two models defined in a models
package.
I'm going to close this ticket. Reopen it you can modify the test case to exactly reproduce your setup and you reproduce the error you report. If so, include a patch or an exact copy of the code. Thanks.
comment:4 by , 15 years ago
Resolution: | worksforme |
---|---|
Status: | closed → reopened |
Ah, the difference appears to be that I am defining the m2m relationship using a string and specifying the app the 'to' model is in. I've attached a very slightly altered version of your diff file that reproduces the same issue for me. I suppose it's possible I'm just never supposed to define an m2m relationship this way? If so, that should probably be documented and/or throw an exception.
comment:5 by , 15 years ago
It seems that 1.2 produces a different column name for the m2m field when there are more than one m2m field in a model. I will attach a sample project that demonstrates this.
by , 15 years ago
Attachment: | test_m2m_column_names.zip added |
---|
Sample project demonstrating the difference in column names for m2m fields
comment:6 by , 15 years ago
Cc: | added |
---|
comment:7 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
On further review, the problem is that I still had the old column name of 'subscriptiontype_id' in my database. Changing the column name to 'member.subscriptiontype_id' does resolve the problem, and syncdb creates the column with the 'correct' name automatically. I am, however, not certain that django should be creating tables with columns with '.' characters in the name automatically...