0

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

Alasdair
  • 298,606
  • 55
  • 578
  • 516
Fahim Ahmed
  • 397
  • 4
  • 16
  • what version are you using?? and why are you not using makemigrations? – Exprator Jun 29 '17 at 06:44
  • I am using Django 1.5 which does not have makemigrations. Upgrading is something which would not happen soon. Is there anyway to tackle this issue without resorting to a version upgrade? – Fahim Ahmed Jun 29 '17 at 06:48
  • just tell me a thing, other field stays or only mammal_type with extinct stays in the new migration file – Exprator Jun 29 '17 at 06:52
  • When I run schema migration for the last field "extinct", it also adds the schema again for "mammal_type" which has already been added in the previous migration. The last migration file forwards function should just have a db.add_column for field extinct, instead it has one for mammal_type as well. – Fahim Ahmed Jun 29 '17 at 07:02
  • try removing the choices and do it again, i guess choice are getting reinitialized everytime thus its being added – Exprator Jun 29 '17 at 07:04
  • Its not related to the choices option, I have checked that. I have received this issue with Boolean fields as well. – Fahim Ahmed Jun 29 '17 at 07:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147897/discussion-between-fahim-ahmed-and-exprator). – Fahim Ahmed Jun 29 '17 at 07:36

0 Answers0