Opened 4 years ago
Closed 3 years ago
#32676 closed Bug (fixed)
Migration autodetector changes related_name for self-referential ManyToManyField.
Reported by: | Mariusz Felisiak | Owned by: | David Wobrock |
---|---|---|---|
Component: | Migrations | Version: | 4.0 |
Severity: | Release blocker | Keywords: | |
Cc: | David Wobrock, Simon Charette | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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).
Change History (9)
comment:1 by , 4 years ago
Component: | Database layer (models, ORM) → Migrations |
---|
comment:2 by , 4 years ago
comment:3 by , 4 years ago
Triage Stage: | Unreviewed → Accepted |
---|
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
for example.
comment:4 by , 4 years ago
Has patch: | set |
---|---|
Needs tests: | set |
Owner: | changed from | to
Patch needs improvement: | set |
Status: | new → assigned |
Started working on a patch https://github.com/django/django/pull/14324
but still Work In Progress
comment:5 by , 3 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | unset |
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!
comment:6 by , 3 years ago
Needs tests: | set |
---|
comment:7 by , 3 years ago
Needs tests: | unset |
---|
comment:8 by , 3 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Hi,
Similarly to #32675, on a first and very quick look, the
ManyToManyField
has some logic atdjango.db.models.fields.related.ManyToManyField.contribute_to_class
that looks very much like the names described in the ticket itself:Since
_meta
should not be available anymore, I expect that the result of theself.remote_field.model == cls._meta.object_name
condition changed, because we should not be able to usecls._meta.app_label
.I'm open to help if we have an idea about how we can fix this :)