-1

I use git in django for my project and my database is postgres. When I make a change in the file and make migrations and then migrate, it is correct and the database is correct.

But when I go back to the previous commit (git checkout command) and then migrate, a new migration is created, but when I run command python manage.py migrate, it says "no changes were found". When I look at the database, I see that it has the same git checkout fields as before.

python manage.py makemigrations
Migrations for 'main_app':
  main_app\migrations\0001_initial.py
    - Create model Project
    - Create model Resume
    - Create model Experience
    - Create model Comment

python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, main_app, sessions, users
Running migrations:
  No migrations to apply.

I went through many ways, I deleted the migrations and created them again, but it didn't help, it still said that there is no change. Only when I delete the database and create it again, the migrate command works correctly. Deleting the database is dangerous.

Can you suggest another way and guide me?

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39

1 Answers1

0

Let me explain the workflow that works best for me:

I develop my project on my local machine (DEBUG=TRUE, built-in dev-server, sqllight3 etc):

  1. I make migrations (python manage.py makemigrations)
  2. I migrate (python manage.py migrate)
  3. I commit the migrations folder but not the database. This database is a dummy database representing the overall scheme of your data and should be deletable at any time if needed.

I deploy/update my project in production:

  1. I do not make migrations here (# python manage.py makemigrations)
  2. I only do migrate (python manage.py migrate).

In case you messed up your code, database or migrations during development, you can always reset:

  1. Delete your dummy database on the local machine
  2. Undo all the changes you did git restore .
  3. Delete potential new migrations (these files are not yet covered by git and are not longer needed since you decided to abandon them)

Now your code base is at the stage of your last working commit. But you are lacking the database.

  1. Now you just want to migrate! (python manage.py migrate) This will create a new database with the scheme you had before. Sure it is missing all the data but exactly for this case it was just dummy data. Usually you have a handy script that populates your database with dummy data at this point.

Reminder: Your dummy data should always represent the data of your project in production! Let's say you have a Product with a model field name. This field was not required, but you want to change it to required.
Your development database does not have a single Product stored. Therefore makemigrations and migrate work just fine. But your production database has Products stored. Some of these Products do not have name since it was not required before. Here the process of makemigrations and migrate will ask you to set a name for the Products that were stored before and now require a name.

It is best to cover this case already during your development stage. Therefore make sure your dummy data is always extensive enough.

Maybe this is an example workflow for you to follow in the future to avoid struggles like the one you are facing right now.

Tarquinius
  • 1,468
  • 1
  • 3
  • 18