Opened 7 years ago

Closed 7 years ago

#28309 closed Bug (needsinfo)

Migration bug in related_name for ManyToManyField with self

Reported by: Ahmed Mohamed Owned by: nobody
Component: Migrations Version: 1.11
Severity: Normal Keywords: ManyToManyField self
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

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

Change History (3)

comment:1 by Tim Graham, 7 years ago

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

comment:2 by Tim Graham, 7 years ago

Description: modified (diff)

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 ManyToManyField.contribute_to_class(). I'm uncertain if this is a bug. Can you please clarify?

comment:3 by Tim Graham, 7 years ago

Resolution: needsinfo
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top