How to reset migration

Keywords: Python Database

How to reset migration

Scenario 1: no need for original database data

  • Delete all tables in the database
  • Delete all files in the migration module of the project, except the init.py file
  • Execution script
python manage.py makemigrations 
python manage.py migrate

Scenario 2: do not want to delete the existing database, just want to re create the migration file

1. First of all, ensure that the current migration file and database are synchronized by executing
python manage.py makemigrations 
No changes detected
2. Execute the command to view all migrations(mock and okr are app names)
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
 [X] 0009_alter_user_last_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
mock
 [X] 0001_initial
 [X] 0002_remove_project_owner
 [X] 0003_project_owner
okr
 [X] 0001_initial
sessions
 [X] 0001_initial
3. Reset migration of all app s
# Execute multiple times, admin is a variable parameter. Get the app name of the last command
python manage.py migrate --fake-initial admin zero
Operations to perform:
  Unapply all migrations: admin
Running migrations:
  Rendering model states... DONE
  Unapplying admin.0002_logentry_remove_auto_add... OK
  Unapplying admin.0001_initial... OK
4. Execute after deleting all files under the migrations folder in the App (except for "init". Py ")
python manage.py makemigrations
python manage.py migrate --fake-initial
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, mock, okr, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK

– fake initial will record the current app execution to 0001 ﹣ initial.py in the migrations table in the database, but it will not actually execute the code in the file. In this way, the existing database will not be changed, and the migration file can be reset. Mom will no longer see a push file in the migration module

Posted by jbloom on Sun, 05 Jan 2020 20:58:59 -0800