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)

12386.diff (1.9 KB ) - added by Ramiro Morales 15 years ago.
Test case demonstrating I can't reproduce this
12386.txt (1.9 KB ) - added by ldevesine 15 years ago.
Slightly altered test case producing the issue
test_m2m_column_names.zip (8.5 KB ) - added by Simon Blanchard 15 years ago.
Sample project demonstrating the difference in column names for m2m fields

Download all attachments as: .zip

Change History (10)

comment:1 by ldevesine, 15 years ago

Component: UncategorizedDatabase layer (models, ORM)

comment:2 by ldevesine, 15 years ago

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...

by Ramiro Morales, 15 years ago

Attachment: 12386.diff added

Test case demonstrating I can't reproduce this

comment:3 by Ramiro Morales, 15 years ago

Resolution: worksforme
Status: newclosed

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.

by ldevesine, 15 years ago

Attachment: 12386.txt added

Slightly altered test case producing the issue

comment:4 by ldevesine, 15 years ago

Resolution: worksforme
Status: closedreopened

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 Simon Blanchard, 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 Simon Blanchard, 15 years ago

Attachment: test_m2m_column_names.zip added

Sample project demonstrating the difference in column names for m2m fields

comment:6 by Simon Blanchard, 15 years ago

Cc: bnomis@… added

comment:7 by Russell Keith-Magee, 15 years ago

Resolution: fixed
Status: reopenedclosed

(In [12226]) Fixed #12386 -- Corrected the column names produced for autogenerated m2m tables when the related table is specified as a dot-separated string. Thanks to ldevesine for the report and simonb for the extra test case.

Note: See TracTickets for help on using tickets.
Back to Top