[Django] #32983: Prevent developers from defining a related_name on symmetrical ManyToManyFields

16 views
Skip to first unread message

Django

unread,
Aug 3, 2021, 12:25:35 AM8/3/21
to django-...@googlegroups.com
#32983: Prevent developers from defining a related_name on symmetrical
ManyToManyFields
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
partofthething |
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: 4.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
In ManyToManyField, if the `symmetrical` argument is passed, or if it's a
self-referential ManyToMany relationship, the related field on the target
model is not created. However, if a developer passes in the `related_name`
not understanding this fact, they may be confused until they find the
information about symmetrical relationship. Thus, it is proposed to raise
an error when the user defines a ManyToManyField in this condition.

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

Django

unread,
Aug 3, 2021, 12:25:59 AM8/3/21
to django-...@googlegroups.com
#32983: Prevent developers from defining a related_name on symmetrical
ManyToManyFields
-------------------------------------+-------------------------------------
Reporter: partofthething | Owner:
Type: | partofthething
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by partofthething):

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


Comment:

I have a PR that implements this incoming.

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

Django

unread,
Aug 3, 2021, 12:37:25 AM8/3/21
to django-...@googlegroups.com
#32983: Prevent developers from defining a related_name on symmetrical
ManyToManyFields
-------------------------------------+-------------------------------------
Reporter: partofthething | Owner:
Type: | partofthething
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by partofthething):

* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/14730

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

Django

unread,
Aug 3, 2021, 3:44:42 AM8/3/21
to django-...@googlegroups.com
#32983: Prevent developers from defining a related_name on symmetrical
ManyToManyFields
-------------------------------------+-------------------------------------
Reporter: Nick Touran | Owner: Nick
Type: | Touran

Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.0
(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 Carlton Gibson):

* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted


Comment:

OK, I guess we can do something here — it probably is a source of
confusion.

The same issue was raised in #18021 (but as an invalid bug report, rather
than suggesting improving the messaging).

Looking at the PR — I'm sceptical about just raising an error — this will
likely break code in the wild.

Can we investigate adding a system check here instead?
There are several similar checks for related fields already:
https://docs.djangoproject.com/en/3.2/ref/checks/#related-fields

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

Django

unread,
Aug 3, 2021, 3:49:03 AM8/3/21
to django-...@googlegroups.com
#32983: Prevent developers from defining a related_name on symmetrical
ManyToManyFields
-------------------------------------+-------------------------------------
Reporter: Nick Touran | Owner: Nick
Type: | Touran
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.0
(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
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson):

Same issue **also** came up in #12641

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

Django

unread,
Aug 3, 2021, 10:09:20 AM8/3/21
to django-...@googlegroups.com
#32983: Prevent developers from defining a related_name on symmetrical
ManyToManyFields
-------------------------------------+-------------------------------------
Reporter: Nick Touran | Owner: Nick
Type: | Touran
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.0
(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
-------------------------------------+-------------------------------------

Comment (by Nick Touran):

Absolutely. A system check is a much better approach than my initial idea
of the error. I have changed the patch to use a system check.

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

Django

unread,
Aug 3, 2021, 10:14:00 AM8/3/21
to django-...@googlegroups.com
#32983: Prevent developers from defining a related_name on symmetrical
ManyToManyFields
-------------------------------------+-------------------------------------
Reporter: Nick Touran | Owner: Nick
Type: | Touran
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.0
(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 Nick Touran):

* needs_better_patch: 1 => 0


Comment:

Unchecking patch needs improvement as instructed on the page, (pending
reviewer acceptance of course).

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

Django

unread,
Aug 4, 2021, 4:16:20 AM8/4/21
to django-...@googlegroups.com
#32983: Prevent developers from defining a related_name on symmetrical
ManyToManyFields
-------------------------------------+-------------------------------------
Reporter: Nick Touran | Owner: Nick
Type: | Touran
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Accepted => Ready for checkin


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

Django

unread,
Aug 6, 2021, 5:57:19 AM8/6/21
to django-...@googlegroups.com
#32983: Prevent developers from defining a related_name on symmetrical
ManyToManyFields
-------------------------------------+-------------------------------------
Reporter: Nick Touran | Owner: Nick
Type: | Touran
Cleanup/optimization | Status: assigned
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson <carlton@…>):

In [changeset:"1fb21ab3770bdc07fc847e131fa252759befadd2" 1fb21ab3]:
{{{
#!CommitTicketReference repository=""
revision="1fb21ab3770bdc07fc847e131fa252759befadd2"
Refs #32983 -- Removed unneeded related_name from test model definitions.
}}}

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

Django

unread,
Aug 6, 2021, 5:57:20 AM8/6/21
to django-...@googlegroups.com
#32983: Prevent developers from defining a related_name on symmetrical
ManyToManyFields
-------------------------------------+-------------------------------------
Reporter: Nick Touran | Owner: Nick
Type: | Touran
Cleanup/optimization | Status: closed

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

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson <carlton@…>):

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


Comment:

In [changeset:"5d4f21b16f002ed5202a68d6977346ccf53b0b0e" 5d4f21b1]:
{{{
#!CommitTicketReference repository=""
revision="5d4f21b16f002ed5202a68d6977346ccf53b0b0e"
Fixed #32983 -- Added system check for redundant related_name on
symmetrical M2M fields.

Since ManyToManyFields defined with `symmetrical=True` do not add a
related field to the target model, including a `related_name` argument
will never do what the coder likely expects. This makes including
a related_name with a symmetrical model raise a system check warning.

ticket-32983
}}}

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

Reply all
Reply to author
Forward
0 new messages