[Django] #24505: Multiple ManyToManyFields to same "to" model with related_name set to '+' mix up badly

10 views
Skip to first unread message

Django

unread,
Mar 19, 2015, 6:50:24 AM3/19/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
----------------------------------------------+--------------------
Reporter: gergelypolonkai | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
I have a model, ProductField, which has a fillable_by and an approvable_by
field. Both fields are defined as

{{{
ManyToManyField(to=Group, related_name='+')
}}}

Now when I add a group to fillable_by and another one to approvable_by,
the fillable_by gets overridden by approvable_by. I cannot find anything
related to this in the documentation, but it seems a pretty bad behaviour
to me.

The problem still exists in latest 1.7.7 version.

--
Ticket URL: <https://code.djangoproject.com/ticket/24505>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 19, 2015, 8:03:30 AM3/19/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------

Reporter: gergelypolonkai | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by bmispelon):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

Hi,

I can confirm that something strange is going on.
I'm attaching a testcase patch to reproduce the issue.

Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:1>

Django

unread,
Mar 19, 2015, 8:03:55 AM3/19/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------

Reporter: gergelypolonkai | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by bmispelon):

* Attachment "issue24505_testcase.diff" added.

Reproduction testcase

Django

unread,
Mar 22, 2015, 9:13:42 AM3/22/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: assigned

Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by marcofucci):

* owner: nobody => marcofucci
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:2>

Django

unread,
Mar 22, 2015, 3:46:35 PM3/22/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: assigned
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by marcofucci):

PR: https://github.com/django/django/pull/4382

"This fixes a bug with multiple many to many fields to the same to model
and related_name set to +.

This happens because Django is still using backwards relations internally
so multiple fields with the same related_name are sometimes overridden and
the last field defined wins.

There is still a bug with Django not throwing an exception when defining
m2m fields with the same related_name and backwards relation enabled but I
think it's slightly different from this one and should be managed
differently.

Ideally we would refactor the ORM so that it doesn't use backwards
relations internally when disabled but this particular fix was easy to
implement (although hard to find) so I would be happy with it.

I had to change a few tests so I would like somebody to take a look at
them and double-check that it makes sense if possible."

Thanks to Marc Tamlyn for the help and bmispelon for the TestCase.

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:3>

Django

unread,
Mar 22, 2015, 3:48:56 PM3/22/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: assigned
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by marcofucci):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:4>

Django

unread,
Mar 23, 2015, 10:04:30 AM3/23/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: assigned
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by marcofucci):

I did a bit of research after `jtiai` pointed me in the right direction
and it seems that we've had the same problem before.

To fix the problem, we documented a workaround where `related_name`s had
to be unique, even the ones ending in `+` #15932.

We then thought we had fixed it in #21375 so we deleted the documented
workaround #21491.

Finally, it seems that the bug is back and addressed here.

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:5>

Django

unread,
Mar 26, 2015, 11:55:55 AM3/26/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: assigned
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 0 => 1


Comment:

Patch needs a rebase.

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:6>

Django

unread,
Mar 26, 2015, 2:55:54 PM3/26/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: assigned
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by marcofucci):

* needs_better_patch: 1 => 0


Comment:

Rebased

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:7>

Django

unread,
Mar 26, 2015, 7:01:08 PM3/26/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: closed

Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"4ee08958f154594b538207a53c1d457687b3f7ae" 4ee08958]:
{{{
#!CommitTicketReference repository=""
revision="4ee08958f154594b538207a53c1d457687b3f7ae"
Fixed #24505 -- Fixed clash with hidden m2m fields.

Added support for multiple m2m fields with the same 'to' model
and with related_name set to '+'.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:8>

Django

unread,
Jun 11, 2015, 7:25:50 PM6/11/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: closed
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by dekkers):

* cc: jeroen@… (added)


Comment:

I just hit this bug with 1.8 and spend a lot of time to figure out what
was going on. Given that the fix is pretty trivial, would it be possible
to backport it to 1.8?

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:9>

Django

unread,
Jun 11, 2015, 7:34:45 PM6/11/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: closed
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

If you'd like to send a pull request, I'll merge it. Please include a
mention in the 1.8.3 release notes.

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:10>

Django

unread,
Sep 30, 2015, 6:45:52 PM9/30/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: closed
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"0e2d3b93043676975aa921a70b5faafef02cac5c" 0e2d3b9]:
{{{
#!CommitTicketReference repository=""
revision="0e2d3b93043676975aa921a70b5faafef02cac5c"
[1.8.x] Fixed #24505 -- Fixed clash with hidden m2m fields.

Added support for multiple m2m fields with the same 'to' model

and with related_name set to '+'.

Backport of 4ee08958f154594b538207a53c1d457687b3f7ae from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:11>

Django

unread,
Sep 30, 2015, 6:47:40 PM9/30/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: closed
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"061801e3dfb3f88550cdaeef1a6dd1c24c13d53d" 061801e3]:
{{{
#!CommitTicketReference repository=""
revision="061801e3dfb3f88550cdaeef1a6dd1c24c13d53d"
Refs #24505 -- Forwardported 1.8.5 release note.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:12>

Django

unread,
Sep 30, 2015, 6:48:09 PM9/30/15
to django-...@googlegroups.com
#24505: Multiple ManyToManyFields to same "to" model with related_name set to '+'
mix up badly
-------------------------------------+-------------------------------------
Reporter: gergelypolonkai | Owner:
| marcofucci
Type: Bug | Status: closed
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"3569e9d4a1e03b46b2649ca1d4fcdf9b72c552d1" 3569e9d4]:
{{{
#!CommitTicketReference repository=""
revision="3569e9d4a1e03b46b2649ca1d4fcdf9b72c552d1"
[1.9.x] Refs #24505 -- Forwardported 1.8.5 release note.

Backport of 061801e3dfb3f88550cdaeef1a6dd1c24c13d53d from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24505#comment:13>

Reply all
Reply to author
Forward
0 new messages