python-3.xdjangodjango-modelsdjango-3.2

Django making migrations


Consider the following three classes:

class UUIDModel(models.Model):

    id = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4)

    class Meta:
        abstract = True


class TimeStampedUUIDModel(UUIDModel):

    created = models.DateTimeField(auto_now_add=True, editable=False)
    modified = models.DateTimeField(auto_now=True, editable=False)

    class Meta:
        abstract = True

class UsefulClass(TimeStampedUUIDModel):

    name = models.CharField(max_length=150, unique=True)
    creator = models.ForeignKey('OtherClass', on_delete=models.SET_NULL, null=True)

Based on these classes, when I am running the makemigrations command, Django will create two migrations (on the very first run). One contains the id, created, modified and name (!) fields and the second one adds the creator foreign key.

What could be the reason of the creation of two migrations, instead of just one?


Solution

  • @PrabinSapal is correct, you could reset your database using python manage.py reset_db --noinput. Django command that resets your Django database, removing all data from all tables. This allows you to run all migrations again.