Schemamigration runs for existing columns in the application

194 Views Asked by At

Assume I am having a Django app named animals. The app has a model named "mammal" as below

class Mammal(models.Model)
    name = models.CharField(max_length=256)
    is_active = models.BooleanField(default=True)
    date_added = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

I run a schema migration for the above model

./manage.py schemamigration mammal --initial

An initial migration file gets created and then I migrate it as follows

./manage.py migrate mammal

Now I update the model and add a field mammal_type as below

class Mammal(models.Model)
    name = models.CharField(max_length=256)
    mammal_type = models.CharField(max_length=63, choices=TYPE_CHOICES, default=TYPE_MAMMAL)
    is_active = models.BooleanField(default=True)
    date_added = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

I again run a schema migration for the above model

./manage.py schemamigration mammal --auto
./manage.py migrate mammal

Everything goes fine. Now comes the issue. I add another field extinct as below

class Mammal(models.Model)
    name = models.CharField(max_length=256)
    mammal_type = models.CharField(max_length=63, choices=TYPE_CHOICES, default=TYPE_MAMMAL)
    extinct = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_added = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

I run a schema migration

./manage.py schemamigration mammal --auto

The new migration file contains schema migration for previously added mammal_type field which actually should not be present. I am not sure why this happens.

I have tried running field specific schema migrations which allows me to add migrations for just that field, but when it comes to creating M2M tables on the fly, field specific schema migrations do not help. I would need to run schema migration for the entire app for the M2M tables to be created.

Please advice

0

There are 0 best solutions below