Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33119 closed Bug (fixed)

Change casing of a model's name referenced by ManyToManyField generates unnecessary migrations.

Reported by: AliGhotbizadeh Owned by: nobody
Component: Migrations Version: 3.2
Severity: Normal Keywords: M2M, RenameModel, deconstruct
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have models like

class DataCenter(models.Model):
    title = models.CharField(max_length=100)

class Project(models.Model):
    datacenter = models.ForeignKey('DataCenter', on_delete=models.CASCADE)

Then I have decided to rename it into "Datacenter" so I was refactored my code and call the "makemigrations" command. But no migration file has created and it was obvious because model names are lowercased in the database schema.
after a while, I have added this model

class Package(models.Model):
   datacenters = models.ManyToManyField('Datacenter')

after creating that model every time I call the "makemigrations" command, the same alter migration has been created.

migrations.AlterField(
            model_name='package',
            name='datacenters',
            field=models.ManyToManyField(related_name='packages', to='operations.Datacenter',
                                         verbose_name='datacenters'),
),

after hours of debugging, I found that in the MigrationLoader's graph, the model name is "DataCenter" because no "RenameModel" migration is even created. then in the "deconstruct" of the M2M field unlike the ForiegnKey field, the "to" attribute is set from "_meta.label" instead of "_meta.label_lower".
and that causes a false change detection in "MigrationAutodetector" every time.

Change History (2)

comment:1 by Mariusz Felisiak, 3 years ago

Resolution: fixed
Status: newclosed
Summary: Change casing of a model's name cause endless creation of migrationsChange casing of a model's name referenced by ManyToManyField generates unnecessary migrations.

Thanks for the report, it was fixed in aa4acc164d1247c0de515c959f7b09648b57dc42 (Django 4.0+).

comment:2 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In b8f3a3a:

Refs #33119 -- Added tests for changing model name case referenced by ManyToManyField.

Fixed in aa4acc164d1247c0de515c959f7b09648b57dc42.

Note: See TracTickets for help on using tickets.
Back to Top