Opened 6 years ago

Closed 6 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: master
Severity: Keywords:
Cc: bnomis@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 6 years ago.
Test case demonstrating I can't reproduce this
12386.txt (1.9 KB) - added by ldevesine 6 years ago.
Slightly altered test case producing the issue
test_m2m_column_names.zip (8.5 KB) - added by simonb 6 years ago.
Sample project demonstrating the difference in column names for m2m fields

Download all attachments as: .zip

Change History (10)

comment:1 Changed 6 years ago by ldevesine

  • Component changed from Uncategorized to Database layer (models, ORM)

comment:2 Changed 6 years ago by ldevesine

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

Changed 6 years ago by ramiro

Test case demonstrating I can't reproduce this

comment:3 Changed 6 years ago by ramiro

  • Resolution set to worksforme
  • Status changed from new to 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.

Changed 6 years ago by ldevesine

Slightly altered test case producing the issue

comment:4 Changed 6 years ago by ldevesine

  • Resolution worksforme deleted
  • Status changed from closed to 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 Changed 6 years ago by simonb

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.

Changed 6 years ago by simonb

Sample project demonstrating the difference in column names for m2m fields

comment:6 Changed 6 years ago by simonb

  • Cc bnomis@… added

comment:7 Changed 6 years ago by russellm

  • Resolution set to fixed
  • Status changed from reopened to closed

(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