[Django] #32676: Migration autodetector changes related_name for self-referential ManyToManyField.

39 views
Skip to first unread message

Django

unread,
Apr 23, 2021, 4:49:20 AM4/23/21
to django-...@googlegroups.com
#32676: Migration autodetector changes related_name for self-referential
ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: Mariusz | Owner: nobody
Felisiak |
Type: Bug | Status: new
Component: Database | Version: 4.0
layer (models, ORM) |
Severity: Release | Keywords:
blocker |
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Migration autodetector no longer adds a model name to the `related_name`
attribute for self-referential `ManyToManyField`, e.g. for a field
{{{
class MyModel2(models.Model):
field_3 = models.ManyToManyField('self')
}}}
it creates a migration with `related_name='field_3_rel_+'` instead of
`related_name='_mymodel2_field_3_+'`.

Regression in aa4acc164d1247c0de515c959f7b09648b57dc42 (see #29899).

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

Django

unread,
Apr 23, 2021, 5:05:34 AM4/23/21
to django-...@googlegroups.com
#32676: Migration autodetector changes related_name for self-referential
ManyToManyField.
----------------------------------+--------------------------------------
Reporter: Mariusz Felisiak | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.0
Severity: Release blocker | 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 Mariusz Felisiak):

* component: Database layer (models, ORM) => Migrations


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

Django

unread,
Apr 24, 2021, 3:15:39 PM4/24/21
to django-...@googlegroups.com
#32676: Migration autodetector changes related_name for self-referential
ManyToManyField.
----------------------------------+--------------------------------------
Reporter: Mariusz Felisiak | Owner: nobody
Type: Bug | Status: new

Component: Migrations | Version: 4.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by David Wobrock):

Hi,

Similarly to #32675, on a first and very quick look, the `ManyToManyField`
has some logic at
`django.db.models.fields.related.ManyToManyField.contribute_to_class` that
looks very much like the names described in the ticket itself:

{{{
def contribute_to_class(self, cls, name, **kwargs):
# To support multiple relations to self, it's useful to have a
non-None
# related name on symmetrical relations for internal reasons. The
# concept doesn't make a lot of sense externally ("you want me to
# specify *what* on my non-reversible relation?!"), so we set it
up
# automatically. The funky name reduces the chance of an
accidental
# clash.
if self.remote_field.symmetrical and (
self.remote_field.model == RECURSIVE_RELATIONSHIP_CONSTANT or
self.remote_field.model == cls._meta.object_name
):
self.remote_field.related_name = "%s_rel_+" % name
elif self.remote_field.is_hidden():
# If the backwards relation is disabled, replace the original
# related_name with one generated from the m2m field name.
Django
# still uses backwards relations internally and we need to
avoid
# clashes between multiple m2m fields with related_name ==
'+'.
self.remote_field.related_name = '_%s_%s_%s_+' % (
cls._meta.app_label,
cls.__name__.lower(),
name,
)
}}}

Since `_meta` should not be available anymore, I expect that the result
of the `self.remote_field.model == cls._meta.object_name` condition
changed, because we should not be able to use `cls._meta.app_label`.

I'm open to help if we have an idea about how we can fix this :)

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

Django

unread,
Apr 26, 2021, 11:59:14 PM4/26/21
to django-...@googlegroups.com
#32676: Migration autodetector changes related_name for self-referential
ManyToManyField.
----------------------------------+------------------------------------

Reporter: Mariusz Felisiak | Owner: nobody
Type: Bug | Status: new

Component: Migrations | Version: 4.0
Severity: Release blocker | 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 Simon Charette):

* stage: Unreviewed => Accepted


Comment:

That seems like a flaw of `RelatedField.deconstruct` tbh, it should not
include `related_name` and friends if it was not specified at
initialization.

I suggest we make `RelatedField.__init__` store `related_name` and
`related_query_name` as `self._related_name` and
`self._related_query_name` and use them instead of relying on
`self.remote_field` in `deconstruct`. That's the approach we've taken for
post-initialization alterable attributes in `Field`
[https://github.com/django/django/blob/4e5bbb6ef2287126badd32842b239f4a8a7394ca/django/db/models/fields/__init__.py#L169
for example].

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

Django

unread,
Apr 28, 2021, 3:26:27 PM4/28/21
to django-...@googlegroups.com
#32676: Migration autodetector changes related_name for self-referential
ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: David
| Wobrock
Type: Bug | Status: assigned

Component: Migrations | Version: 4.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

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

* owner: nobody => David Wobrock
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* status: new => assigned
* needs_tests: 0 => 1


Comment:

Started working on a patch https://github.com/django/django/pull/14324
but still Work In Progress

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

Django

unread,
May 26, 2021, 6:43:03 AM5/26/21
to django-...@googlegroups.com
#32676: Migration autodetector changes related_name for self-referential
ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: David
| Wobrock
Type: Bug | Status: assigned
Component: Migrations | Version: 4.0
Severity: Release blocker | 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 David Wobrock):

* needs_better_patch: 1 => 0
* needs_tests: 1 => 0


Comment:

Some discussions occurred on the Pull Request:
https://github.com/django/django/pull/14324#issuecomment-843038110

Added tests and updated release note, patch is ready for review :) Thanks!

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

Django

unread,
May 27, 2021, 1:12:58 AM5/27/21
to django-...@googlegroups.com
#32676: Migration autodetector changes related_name for self-referential
ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: David
| Wobrock
Type: Bug | Status: assigned
Component: Migrations | Version: 4.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

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

* needs_tests: 0 => 1


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

Django

unread,
May 27, 2021, 11:05:28 AM5/27/21
to django-...@googlegroups.com
#32676: Migration autodetector changes related_name for self-referential
ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: David
| Wobrock
Type: Bug | Status: assigned
Component: Migrations | Version: 4.0
Severity: Release blocker | 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 David Wobrock):

* needs_tests: 1 => 0


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

Django

unread,
May 28, 2021, 3:20:48 AM5/28/21
to django-...@googlegroups.com
#32676: Migration autodetector changes related_name for self-referential
ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: David
| Wobrock
Type: Bug | Status: assigned
Component: Migrations | Version: 4.0
Severity: Release blocker | 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 Mariusz Felisiak):

* stage: Accepted => Ready for checkin


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

Django

unread,
May 29, 2021, 6:47:17 AM5/29/21
to django-...@googlegroups.com
#32676: Migration autodetector changes related_name for self-referential
ManyToManyField.
-------------------------------------+-------------------------------------
Reporter: Mariusz Felisiak | Owner: David
| Wobrock
Type: Bug | Status: closed
Component: Migrations | Version: 4.0
Severity: Release blocker | 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 Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"b9df2b74b98b4d63933e8061d3cfc1f6f39eb747" b9df2b7]:
{{{
#!CommitTicketReference repository=""
revision="b9df2b74b98b4d63933e8061d3cfc1f6f39eb747"
Fixed #32676 -- Prevented migrations from rendering related field
attributes when not passed during initialization.

Thanks Simon Charette for the implementation idea.
}}}

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

Reply all
Reply to author
Forward
0 new messages