#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:

> 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')),
>     ],
> ),
> }}}

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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.2d26ec44ba20835577d7afe4ce21b1f9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to