您的位置:首页 > 脚本大全 > > 正文

django settings配置(Django重置migrations文件的方法步骤)

更多 时间:2021-10-11 00:30:19 类别:脚本大全 浏览量:2992

django settings配置

Django重置migrations文件的方法步骤

django开发过程中如果数据库变动过多导致migrations的文件越来越多,管理起来很不方便, 幸运的是django提供了一种方式可以是这些文件重置到0001状态,而且不删除原有数据。

确认migration文件跟数据库同步

  • ?
  • 1
  • $ python3 manage.py makemigrations
  • 如果提示 no changes detected 那么数据就是同步的。

    查看当前migration文件记录

  • ?
  • 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
  • 重置文件

  • ?
  • 1
  • python3 manage.py migrate --fake mysite zero # mysite是app的名称
  • 删除migrations的处init.py的其他文件

    重新生产migrate文件

  • ?
  • 1
  • $ python3 manage.py makemigrations
  • 同步到数据库

  • ?
  • 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是假冒伪装的意思。执行过程,但不应用数据。

    这些就清爽多了。

    场景一

    不考虑数据库数据,可以完全清空数据库。

    步骤:

    删除所有migrations

  • ?
  • 1
  • 2
  • find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
  • find . -path "*/migrations/*.pyc" -delete
  • 删除数据库

    重新生成migrations

  • ?
  • 1
  • 2
  • python manage.py makemigrations
  • python manage.py migrate
  • 场景二

    有时候我们会直接导入完整的数据库,包括数据,这种情况下就不能简单的清空数据库。

    这时我们的目的就是:清空数据库的migration history,保证以后的migrate能正常使用,但要保留其他数据。

    步骤:

    从数据库中删除所有非0001_initial的migration history

  • ?
  • 1
  • delete from django_migrations where app in ('your','app','labels') and name != '0001_initial'
  • 使用migrate命令回滚0001_initial的migration history

  • ?
  • 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已是最新的,可跳过此步

  • ?
  • 1
  • 2
  • 3
  • 4
  • find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
  • find . -path "*/migrations/*.pyc" -delete
  •  
  • python manage.py makemigrations
  • 在数据库中生成新的0001_initial记录

  • ?
  • 1
  • python migrate --fake-initial
  • 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。

    原文链接:http://litets.com/article/2019/4/30/389.html

    您可能感兴趣