How To Force Reset Django Models Migrations

During the process of develop example code in article Django Bootstrap3 Example, when i change the model class source code ( for example add a new field ), i always want to migrate the changes to the backend database table immediately. But i found sometimes the migration do not take effect when the table exist, this article will tell you how to force reset all the tables and re-migrate the Django model classes.

1. Force Reset Django App Model Migrations Steps.

  1. Delete all the Django app tables from the SQLite3 database. I use SQLiteStudio to do it, you can use your favorite database tool to do it also. From below picture, we can see that the table name with prefix dept_emp_ is the tables used in the dept_emp Django app. The third table dept_emp_employee_dept is because we change the Employee.dept field to models.ManyToMany type, so need this table to record the many to many relationship between Employee and Department. You can refer article How To Operate Foreign Key And Many To Many Field In Django Model.
    django dept_emp app backend tables
  2. Open a terminal and cd into the Django project root folder then run below command to list Django application migrations history. The X character means the migration has been applied.
    192:DjangoHelloWorld zhaosong$ python3 manage.py showmigrations dept_emp
    
    dept_emp
     [X] 0001_initial
  3. Clear the Django application’s migration history with migrate app_name zero command like below. This command will delete all the Django application related backend tables also.
    192:DjangoHelloWorld zhaosong$ python3 manage.py migrate dept_emp zero
    
    Operations to perform:
      Unapply all migrations: dept_emp
    Running migrations:
      Rendering model states... DONE
      Unapplying dept_emp.0001_initial... OK
  4. Show the Django app migration history again, you will now see that the X character before the migration history has been removed this means this migration is not applied.
    192:DjangoHelloWorld zhaosong$ python3 manage.py showmigrations dept_emp
    
    dept_emp
     [ ] 0001_initial
  5. Now go to the app migrations folder ( DjangoHelloWorld / dept_emp / migrations ) and only reserver the __init__.py file and  remove all other .py file in it. If you delete the __init__.py file by accident, do not worry, just create an empty __init__.py file in the migrations folder.
    django project app dept_emp source files structure
  6. Now run showmigrations dept_emp command again. You can find there has no migrations for this Django application.
    192:DjangoHelloWorld zhaosong$ python3 manage.py showmigrations dept_emp
    
    dept_emp
     (no migrations)
  7. If you remove the __init__.py file in migrations folder and run showmigrations command, then you will get below errors.
    192:DjangoHelloWorld zhaosong$ python3 manage.py showmigrations dept_emp
    
    CommandError: No migrations present for: dept_emp
  8. Run makemigrations dept_emp command to initialize the migrations.
    192:DjangoHelloWorld zhaosong$ python3 manage.py makemigrations dept_emp
    
    Migrations for 'dept_emp':
      dept_emp/migrations/0001_initial.py
        - Create model Department
        - Create model Employee
        - Alter unique_together for department (1 constraint(s))
        - Alter unique_together for employee (1 constraint(s))
  9. Run showmigrations dept_emp command again to see the added but not applied migrations.
    192:DjangoHelloWorld zhaosong$ python3 manage.py showmigrations dept_emp
    
    dept_emp
     [ ] 0001_initial
  10. Run migrate command to apply the migrations, after this the backend tables will be created again without any data.
    192:DjangoHelloWorld zhaosong$ python3 manage.py migrate dept_emp
    
    Operations to perform:
      Apply all migrations: dept_emp
    Running migrations:
      Applying dept_emp.0001_initial... OK
READ :   What Does _ Or __ Means In Python

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.