0

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?

Infinite Possibilities
  • 7,415
  • 13
  • 55
  • 118
  • 1
    Hi there, it can only be in case you have already run makemigrations first time with name field only and later added creator field in UsefulClass and you again run makemigrations. But If you run makemigrations for the first time with both fields as above model then it won't create two different migration file. – Prabin Sapal Dec 08 '21 at 12:31
  • Hello, exactly this is the behaviour I am waiting for, but in my case, I deleted all the migrations and rerun the command and still created 2 migrations. Any clues? – Infinite Possibilities Dec 08 '21 at 12:34
  • 1
    Hello there, it only creating one migration when i tried in my system with your same above code. Can you also try deleting the database or connecting to new fresh database and also delete all migrations from all apps and run makemigrations again – Prabin Sapal Dec 08 '21 at 12:47
  • Sure, I will do this. – Infinite Possibilities Dec 08 '21 at 12:59
  • @PrabinSapal, I deleted the migration folder (before I deleted just the migration files only). Deleted the database and recreated. This time it was necessary to add the app name too after the `makemigrations` command. After executing this time the command, only one migration was created. Thank you for your help. I am still not sure why it created two files when the folder wasn't deleted. – Infinite Possibilities Dec 08 '21 at 13:25

1 Answers1

1

@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.

user11717481
  • 1
  • 9
  • 15
  • 25
  • I cannot test this solution, because I cannot reproduce the issue anymore, but it seems to be a legit solution, since my solution was to delete the database and recreate it. – Infinite Possibilities Dec 15 '21 at 15:01