How To Force Reset Django Models Migrations

During the process of developing the example code in the 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 does not take effect when the table exists, 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.
  2. From the below picture, we can see that the table name with prefix dept_emp_ is the table used in the dept_emp Django app.
  3. The third table dept_emp_employee_dept is because we change the Employee.dept field to models.ManyToMany type, so we need this table to record the many to many relationships between Employee and Department.
  4. You can refer article How To Operate Foreign Key And Many To Many Field In Django Model.
    django-dept_emp-app-backend-tables
  5. Open a terminal and cd into the Django project root folder then run the 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
  6. Clear the Django application’s migration history with the command migrate app_name zero 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
  7. Show the Django app migration history again, you will now see that the X character before the migration history has been removed which means this migration is not applied.
    192:DjangoHelloWorld zhaosong$ python3 manage.py showmigrations dept_emp
    
    dept_emp
     [ ] 0001_initial
  8. Now go to the app migrations folder ( DjangoHelloWorld / dept_emp / migrations ) and only reserver the __init__.py file and remove all other .py files in it.
  9. If you delete the __init__.py file by accident, do not worry, just create an empty __init__.py file in the migrations folder.
  10. Now run showmigrations dept_emp command again. You can find there have no migrations for this Django application.
    192:DjangoHelloWorld zhaosong$ python3 manage.py showmigrations dept_emp
    
    dept_emp
     (no migrations)
  11. If you remove the __init__.py file in the migrations folder and run the command showmigrations, then you will get the below errors.
    192:DjangoHelloWorld zhaosong$ python3 manage.py showmigrations dept_emp
    
    CommandError: No migrations present for: dept_emp
  12. Run the command makemigrations dept_emp 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))
  13. Run the command showmigrations dept_emp again to see the added but not applied migrations.
    192:DjangoHelloWorld zhaosong$ python3 manage.py showmigrations dept_emp
    
    dept_emp
     [ ] 0001_initial
  14. Run the command migrate 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
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x