How to delete files under migrations without causing any errors

Posted by twilightnights on Wed, 12 Jan 2022 02:29:53 +0100

Reproduced at: https://blog.csdn.net/gaifuxi9518/article/details/86591806

1. Data sheets are not considered
First, delete the data table under the relevant APP in the database
Then delete all files in the migration module under APP, except init Py file
Execute the following command

python manage.py makemigrations
python manage.py migrate

2. Retention of the original data sheet
This is the most common situation in development, and it is also a slightly complex situation. However, as long as the following operation steps are followed, no errors will be caused:

1. Check whether the modification record file under the migrations folder is consistent with the database. If we execute this command

python manage.py makegrations

The execution result shows:

No changes detected

Then we can continue with the following steps

2. View the effective (successfully executed) migration files corresponding to all apps under the current project. The commands are as follows:

python manage.py showmigrations

The results are shown in the figure below. The previous [x] indicates the files that have been executed, such as two operations under the imooc APP

admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
imooc
[X] 0001_initial
[X] 0002_auto_20190122_0929
sessions
[X] 0001_initial

3. Reset your APP operations to restore them to the unexecuted state. Here, notice that the symbol in front of fake is two "-". In addition, imooc is the name of the APP. Remember to replace it with your APP name. Another point is not to copy directly, but to knock on the terminal by hand so as not to cause errors due to format problems. How to understand the fake parameter? Fake means pretending. Here, we can understand it as assuming that we set these operations to the state of no execution, rather than that they really don't execute!

python manage.py migrate --fake imooc zero

If the result is as follows, the reset is successful

(wprkplace) D:\PythonProject\learn>python manage.py migrate --fake imooc zero
Operations to perform:
Unapply all migrations: imooc
Running migrations:
Rendering model states... DONE
Unapplying imooc.0005_hua... FAKED
Unapplying imooc.0004_xiao... FAKED
Unapplying imooc.0003_ming... FAKED
Unapplying imooc.0002_auto_20190122_0929... FAKED
Unapplying imooc.0001_initial... FAKED

Then we can execute the command show migrations again to check the status of each operation

(wprkplace) D:\PythonProject\learn>python manage.py showmigrations
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
imooc
[ ] 0001_initial
[ ] 0002_auto_20190122_0929
[ ] 0003_ming
[ ] 0004_xiao
[ ] 0005_hua
sessions
[X] 0001_initial

You can see that all the operations under the imooc APP are reset to the status of not executed, [x] becomes []. Note that it is assumed that it is not executed!

4. Then safely and boldly delete the migration folder, except__ init__.py files, all py files with serial numbers, including pycache folder!

5. Execute the following command to generate 0001 for this APP again_ initial. Py and other files

python manage.py makemigrations

If the prompt is as follows, it is successful

(wprkplace) D:\PythonProject\learn>python manage.py makemigrations
Migrations for 'imooc':
imooc\migrations\0001_initial.py:
- Create model Hua
- Create model Ming
- Create model Post
- Create model Tag
- Create model Test
- Create model Xiao

6. Execute the following command to make the 0001 just generated_ initial. Record py file to django_ In the migrations data table, this table is automatically generated by django and is used to record the database modification operations performed each time. There are four fields in the table. name and app are the corresponding relationship, which means the so and so operation under so and so app.

python manage.py migrate --fake-initial

The result is success

(wprkplace) D:\PythonProject\learn>python manage.py migrate --fake-initial
Operations to perform:
Apply all migrations: admin, auth, contenttypes, imooc, sessions
Running migrations:
Applying imooc.0001_initial... FAKED

If you look at Django again_ In the migrations table, you will find that a record is added to the table. Generally, such a record will be generated only if we really perform the database modification operation, but we use the "fake" operation of fake, so in fact, we only add a record without actually modifying the data table, Finished resetting the migrations folder!

Topics: Django