[Django] #28309: Migration bug in related_name for ManyToManyField with self

6 views
Skip to first unread message

Django

unread,
Jun 14, 2017, 11:52:57 PM6/14/17
to django-...@googlegroups.com
#28309: Migration bug in related_name for ManyToManyField with self
-------------------------------------+-------------------------------------
Reporter: Ahmed | Owner: nobody
Mohamed |
Type: Bug | Status: new
Component: | Version: 1.11
Migrations | Keywords: ManyToManyField
Severity: Normal | self
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The related_name is not set correctly on ManyToManyField when it is
related with 'self'. When I use the model name explicitly, it is set
correctly.


{{{
class Model1(models.Model):
children = models.ManyToManyField('self', blank=True,
related_name='parents')

class Model2(models.Model):
children = models.ManyToManyField('self', blank=True,
related_name='parents')
}}}


{{{
migrations.CreateModel(
name='Model1',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID')),
('children', models.ManyToManyField(blank=True,
related_name='_model1_children_+', to='raftprot.Model1')),
],
),
migrations.CreateModel(
name='Model2',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID')),
('children', models.ManyToManyField(blank=True,
related_name='parents', to='raftprot.Model2')),
],
),
}}}

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

Django

unread,
Jun 15, 2017, 9:06:02 AM6/15/17
to django-...@googlegroups.com
#28309: Migration bug in related_name for ManyToManyField with self
-------------------------------------+-------------------------------------
Reporter: Ahmed Mohamed | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.11
Severity: Normal | Resolution:
Keywords: ManyToManyField | Triage Stage:
self | Unreviewed
Has patch: 0 | Needs documentation: 0

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

Comment (by Tim Graham):

I think there's a mistake in the ticket description. The two `children`
fields in the models are identical.

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

Django

unread,
Jun 15, 2017, 1:39:34 PM6/15/17
to django-...@googlegroups.com
#28309: Migration bug in related_name for ManyToManyField with self
-------------------------------------+-------------------------------------
Reporter: Ahmed Mohamed | Owner: nobody
Type: Bug | Status: new

Component: Migrations | Version: 1.11
Severity: Normal | Resolution:
Keywords: ManyToManyField | Triage Stage:
self | Unreviewed
Has patch: 0 | Needs documentation: 0

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

Old description:

New description:

The related_name is not set correctly on ManyToManyField when it is
related with 'self'. When I use the model name explicitly, it is set
correctly.


{{{
class Model1(models.Model):
children = models.ManyToManyField('self', blank=True)

class Model2(models.Model):
children = models.ManyToManyField('self', blank=True,
related_name='parents')
}}}


{{{
migrations.CreateModel(
name='Model1',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID')),
('children', models.ManyToManyField(blank=True,
related_name='_model1_children_+', to='raftprot.Model1')),
],
),
migrations.CreateModel(
name='Model2',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,
serialize=False, verbose_name='ID')),
('children', models.ManyToManyField(blank=True,
related_name='parents', to='raftprot.Model2')),
],
),
}}}

--

Comment (by Tim Graham):

I've edited the ticket description as I think you meant, please correct it
if needed. If the unexpected output in the migration is
`related_name='_model1_children_+'`, that's coming from
[https://github.com/django/django/blob/a6b5321ce997b899e540bb427cca98cb2b93a106/django/db/models/fields/related.py#L1531-L1546
ManyToManyField.contribute_to_class()]. I'm uncertain if this is a bug.
Can you please clarify?

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

Django

unread,
Jun 17, 2017, 8:29:16 AM6/17/17
to django-...@googlegroups.com
#28309: Migration bug in related_name for ManyToManyField with self
-------------------------------------+-------------------------------------
Reporter: Ahmed Mohamed | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.11
Severity: Normal | Resolution: needsinfo

Keywords: ManyToManyField | Triage Stage:
self | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* status: new => closed
* resolution: => needsinfo


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

Reply all
Reply to author
Forward
0 new messages