The ultimate solution to django cleanup migration

The ultimate solution to django cleanup migration
Home   >   linux   >   Django    Author: littleQ   Thursday, September 14, 2017 at 9:47   Heat: 1081°   Font size: small   , medium and   large   Comments: 0

1. The process of django generating data table structure

       After we design the models, we can generate the data structure file that will be synchronized to the database with the following command

       python manage.py makemigrations

       The generated files are files starting with 000* in the migrations folder in the app directory.

       Every time you run makemigrations, django will generate a new file in the migrations folder based on the changes to the models.

       Then we run python manage.py migrate, and django will generate a file based on the migrations folder to write the data structure to the database.

       So does it repeat the execution of all the files in the migrations every time? Of course not, every time migrate is executed, django will record the migrations file that has been executed in the django_migrations data table. Go to the database and query the table to see the migrations executed in the corresponding APP.


2. How to rebuild the table after django has dealt with the table in the database?

       After we understand the process of django to generate the table structure in the database, then how to clean it is very simple.

       The first step, we need to clean up all files in the migrations folder except the __init__.py file. (Of course, we can also consider modifying this file directly during partial cleaning.)

       In the second step, we need to clean up the migrations records under the app corresponding to django_migrations in the database.

       After cleaning, we will do it again

        python manage.py makemigrations and python manage.py migrate

       The table structure file can be regenerated.


3. Django cleanup migrations accessible version

       Cleaning up migrations requires cleaning up two tags.

       a. Delete all files except __init__.py in the migrations folder in the app.

       b. Log in to the database, find the django_migrations table, and then delete the migrations record under the corresponding app in the table.



Forwarded from: http://www.hylinux.cn/Django/87.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325572405&siteId=291194637