0

I have 5 migration files created. But when I run ./manage.py migrate it always tries to apply the migrations file "3". Even though the latest one is file 5.

How can I fix this issue?

I have tried:

./manage.py makemigrations app_name
./manage.py migrate app_name
./manage.py migrate --run-syncdb

Also, I checked the dbshell, and there is a table already created for the model which is part of migrations file 5.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Ankit Singh
  • 117
  • 1
  • 7
  • Migrations won't run unless something has been changed on the model (altered schema). Because Django is smart enough to create a table only the very first time you run migration, and won't run again as I mentioned already - if something has been altered on the model. Or you can do something like this -> https://stackoverflow.com/a/69922730/1737811 – mutantkeyboard Nov 24 '22 at 13:28
  • The naming of the migration files don't actually matter, in the migration file itself Django usually mentions the dependencies of the migration (i.e. which migration you should have run before). With version control and multiple people working on a project it might happen that a file with `0003` prefix got generated in a branch. Can you show the contents of your migration files (mainly the dependencies, etc.)? Have a look at the [documentation](https://docs.djangoproject.com/en/4.1/topics/migrations/#version-control) – Abdul Aziz Barkat Nov 25 '22 at 08:31

2 Answers2

0

Simple thing, because you didn't use migration file value while doing makemigrations. And migration file value is 0005. You must specify that value while doing makemigrations.

Use these three commands for migrations:

python manage.py makemigrations appname

python manage.py sqlmigrate appname 0005 #specified that migration file 5 value here

python manage.py migrate

Now migrations will apply on that migration file 5 using its value 0005

Manoj Tolagekar
  • 1,816
  • 2
  • 5
  • 22
  • `sqlmigrate` just shows you the SQL that will be run for the migration, its good to review but running that won't make any difference... – Abdul Aziz Barkat Nov 25 '22 at 08:27
0

I ended up deleting the migration file 3 which was getting picked up by django and add the operations of migration file 3 to inital file. Then when I ran migrate <app_name>, it picked up the last file (file 5). I did have to resolve some conflicts between file 3 and preceding files though

Ankit Singh
  • 117
  • 1
  • 7