Django开发:重置migrations文件到0001状态的方法步骤

2023-01-1815:40:38后端程序开发Comments1,008 views字数 2037阅读模式

Django开发过程中如果数据库变动过多导致migrations的文件越来越多,管理起来很不方便, 幸运的是Django提供了一种方式可以是这些文件重置到0001状态,而且不删除原有数据。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

确认migration文件跟数据库同步
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
$ python3 manage.py makemigrations

如果提示 No changes detected 那么数据就是同步的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

查看当前migration文件记录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
$ python3 manage.py showmigrations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
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
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
isite
 [X] 0001_initial
 [X] 0002_article_pub_date
sessions
 [X] 0001_initial

重置文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
python3 manage.py migrate --fake mysite zero # mysite是app的名称

删除migrations的处init.py的其他文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

重新生产migrate文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
$ python3 manage.py makemigrations

同步到数据库文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
python3 manage.py migrate --fake-initial
1
2
3
4
Operations to perform:
 Apply all migrations: admin, auth, contenttypes, isite, sessions
Running migrations:
 Applying isite.0001_initial... FAKED

fake是假冒伪装的意思。执行过程,但不应用数据。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

这些就清爽多了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

场景一文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

不考虑数据库数据,可以完全清空数据库。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

步骤:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

删除所有migrations文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
2
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete

删除数据库文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

重新生成migrations文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
2
python manage.py makemigrations
python manage.py migrate

场景二文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

有时候我们会直接导入完整的数据库,包括数据,这种情况下就不能简单的清空数据库。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

这时我们的目的就是:清空数据库的migration history,保证以后的migrate能正常使用,但要保留其他数据。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

步骤:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

从数据库中删除所有非0001_initial的migration history文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
DELETE FROM django_migrations WHERE app IN ('your','app','labels') AND name != '0001_initial'

使用migrate命令回滚0001_initial的migration history文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
2
3
python manage.py migrate --fake your zero
python manage.py migrate --fake app zero
python manage.py migrate --fake labels zero

重新生成0001_initial,如果能保证已有0001_initial已是最新的,可跳过此步文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
2
3
4
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
python manage.py makemigrations

在数据库中生成新的0001_initial记录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html

1
python migrate --fake-initial
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30514.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/30514.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定