如果您的其他代码已经引用了需要被重置的模型,这会稍微棘手一些,因为在您执行
makemigrations
的时候,会检查各个代码的依赖等是否正常,如果简单的删除模型,可能导致makemigrations无法完整执行
这样,就需要一步步将所有相关代码都注释掉,等到模型重置后,再将这些代码恢复(撤销屏蔽)
可能出现的地方有(将DRF一同考虑)
urls.py
serializer.py
views.py
可以将这些文件都全部注释掉,模型重置完毕后,再解开注释
python manage.py showmigrations <appName>
查看当前迁移执行情况python manage.py migrate <appName> <00xxx(replace it with your target migration file name code>
showmigrations
检查撤销情况showmigrations
检查撤销情况python manage.py makemigrations <appName>
python manage.py migrate <appName>
# 执行迁移情况查看(当前的应用(django app)是user应用)
python manage.py showmigrations user
[X] 0013_alter_wordsearchhistory_spelling_and_more
[X] 0014_testforeignkey
[X] 0015_delete_wordstar
[X] 0016_wordstar
[X] 0017_alter_user_signupdate
[X] 0018_user_openid
[ ] 0019_alter_user_openid
python manage.py migrate 0017
0017
python manage.py migrate user 0017
Operations to perform:
Target specific migration: 0017_alter_user_signupdate, from user
Running migrations:
Rendering model states... DONE
Unapplying user.0018_user_openid... OK
[X] 0016_wordstar
[X] 0017_alter_user_signupdate
[ ] 0018_user_openid
[ ] 0019_alter_user_openid
PS D:\repos\ELA\backEnd\ela> manage migrate scoreImprover 0001 --fake
Operations to perform:
Target specific migration: 0001_initial, from scoreImprover
Running migrations:
Rendering model states... DONE
Unapplying scoreImprover.0002_alter_cet4study_wid_alter_cet6study_wid... FAKED
可以看到,0002
那一次迁移被伪撤回了
PS D:\repos\ELA\backEnd\ela> pmg showmigrations scoreImprover
scoreImprover
[X] 0001_initial
[ ] 0002_alter_cet4study_wid_alter_cet6study_wid
PS D:\repos\ELA\backEnd\ela> .\manage.py migrate --fake -h
usage: manage.py migrate [-h] [--noinput] [--database DATABASE] [--fake] [--fake-initial] [--plan] [--run-syncdb]
[--check] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH]
[--traceback] [--no-color] [--force-color] [--skip-checks]
[app_label] [migration_name]
Updates database schema. Manages both apps with migrations and those without.
positional arguments:
app_label App label of an application to synchronize the state.
migration_name Database state will be brought to the state after that migration. Use the name "zero" to
unapply all migrations.
options:
-h, --help show this help message and exit
--noinput, --no-input
Tells Django to NOT prompt the user for input of any kind.
--database DATABASE Nominates a database to synchronize. Defaults to the "default" database.
--fake Mark migrations as run without actually running them.
--fake-initial Detect if tables already exist and fake-apply initial migrations if so. Make sure that
the current database schema matches your initial migration before using this flag. Django
will only check for an existing table name.
--plan Shows a list of the migration actions that will be performed.
--run-syncdb Creates tables for apps without migrations.
--check Exits with a non-zero status if unapplied migrations exist.
--version Show program's version number and exit.
-v {0,1,2,3}, --verbosity {0,1,2,3}
Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose
output
--settings SETTINGS The Python path to a settings module, e.g. "myproject.settings.main". If this isn't
provided, the DJANGO_SETTINGS_MODULE environment variable will be used.
--pythonpath PYTHONPATH
A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
--traceback Raise on CommandError exceptions.
--no-color Don't colorize the command output.
--force-color Force colorization of the command output.
--skip-checks Skip system checks.
--fake Mark migrations as run without actually running them.
--fake-initial Detect if tables already exist and fake-apply initial migrations if so. Make sure that
the current database schema matches your initial migration before using this flag. Django
First, to clear migrations table:(清理migrations 表,注意这里直接用的是
migrate
而非makemigrations
)./manage.py migrate --fake <app-name> zero
Remove
app-name/migrations/
folder or contents.Make the migrations:
./manage.py makemigrations <app-name>
Finally tidy up(整理) your migrations without making other database changes:
./manage.py migrate --fake <app-name>