If you want to know how to delete files under migrations without causing any errors, then you should read the follow content.

When we doing the Django development operation database, every modification we make will be recorded in the migration directory of the app directory in the form of files.

However as the files increasing , it is not conducive to our later maintenance. So we need regularly clean (reset) the migrations folder.

This article will elegantly tell you how to silently delete files under migrations without causing any errors!

1. Regardless of the data table

  • First delete the data table under the relevant APP in the database
  • Then delete all the files in the migration module under the app, except the init.py file.
  • Execute the following command:
python manage.py makemigrations
python manage.py migrate


2. Retain the original data sheet

This situation is the most common in development and a bit more complicated to operate. But as long as you follow the steps below, it will not cause any errors:

  • 1. Check if the modification record file under the migrations folder is consistent with the database, if we execute this command.
python manage.py makemigrations

The execution result shows:

No  changes  detected

Then we can continue to perform the following steps.

  • 2. View the migration files that have been valid (already executed successfully) for all APPs under the current project. The commands are as follows:
python manage.py showmigrations

The result is shown in the figure below. The previous [x] indicates the file that has been executed. For example, imooc has performed two operations under the 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’s operation to restore them to the state where they are not executed.

Note the symbols in front of fake, which are two “-“s. In addition, imooc is the name of the APP. Remember to replace it with your app. first name. 

Another point is that you don’t copy it directly. You don’t have to type the terminal to make a mistake because of formatting problems. 

How to understand the fake parameter? 

Fake is pretending, here we can understand that we assume that we set these operations to a state of no execution, rather than they really did not execute!

python manage.py migrate --fake imooc zero

The result is as follows, the reset is displayed successfully.

(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 showmigrations command again to see 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

It can be seen that the operations below the imooc app are all reset to the state of no execution, [x] becomes [ ], and the assumption is that it is not executed!

  • 4. Then feel free to delete the migrations folder below, except for the __init__.py file, all the py files with serial numbers, including the pycache folder!
  • 5. Execute the following command to generate a file like 0001_initial.py for this app again.
python manage.py makemigrations

The prompt is as follows

wprkplace) D:\PythonProject\learn>python manage.py makemigrations
Migrations for 'imooc':
  imooc\migrations
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
01_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 record the newly generated 0001_initial.py file into the django_migrations data table.

This table is automatically generated by django and used to record the database modification operation for each execution.

The table has four fields, name and The app is the corresponding relationship, meaning a certain operation under a certain APP.

python manage.py migrate --fake-initial

The result is as follows

(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 the django_migrations table again, you will find that this table adds a record. Generally speaking, if we actually perform the database modification operation, we will generate such a record. But we use fake “pretend” operation.

In fact, we just added a record only, and did not actually modify the data table, that is, complete the reset of the migrations folder!

So, you have learned how to delete files under migrations without causing any errors now.