Home > Software design >  Django migrations dont work. Have to delete the db and start fresh eveery time
Django migrations dont work. Have to delete the db and start fresh eveery time

Time:11-08

I have a django project which works fine. However, each time there is a change in my models (when I add attributes to my model), the migrations stop working. There is usually some sort of error or the migrations will not execute. The only way for me to make the migrations to work is to drop the database and start fresh. This is a very strange behavior and cannot work in a live/production environment. I recently had to delete the db in the production environment and it was very messy.

Is there a way to fix this ? Isnt this strange that Django makes it harder to work with migrations when it claims that migrations make everything easy. Am I doing something wrong ? I dont know where to start from.

CodePudding user response:

Before you run python manage.py migrate, are you running python manage.py makemigrations?

CodePudding user response:

First thing, you don't need to delete your db when there is something wrong with migrations.

When you edit something in models then you need to migrate manually using below command:

python manage.py makemigrations appname

python manage.py sqlmigrate appname 0001 #This value will come after makemigrations, it can be either 0001 or 0002 or 0003 and so on.

python manage.py migrate

CodePudding user response:

I am assuming you have run the following command sequentially.

  1. python manage.py makemigrations
  2. python manage.py migrate

If you are getting migrations-related errors then please read the error carefully.

Mainly this type of migrations-related error occurs for the inconsistency in your migrations file. This is not a proper solution you are just dropping your database every time. Firstly you should be more concerned while writing models. Lastly, you can modify your migrations file to remove inconsistency or cross-check django_migrations table with your migrated migrations. Best of luck :)

  • Related