zoukankan      html  css  js  c++  java
  • makemigrations migrate


    如何重置迁移

    如何重置迁移(图片:https//www.pexels.com/photo/sky-flying-animals-birds-1209/

    Django迁移系统的开发和优化使其能够进行大量迁移。通常,您不应该介意在代码库中保留大量模型迁移。即使有时它会导致一些不良影响,比如在运行测试时耗费大量时间。但在这种情况下,您可以轻松禁用迁移(尽管目前没有内置选项)。

    无论如何,如果你想进行清理,我将在本教程中提供一些选项。


    场景1:

    该项目仍处于开发环境中,您希望执行完整清理。你不介意把整个数据库扔掉。

    1.删​​除项目中的所有迁移文件

    浏览每个项目应用程序迁移文件夹并删除内部的所有内容,__init__.py文件除外

    或者,如果您使用类似Unix的操作系统,则可以运行以下脚本(在项目目录中):

    find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
    find . -path "*/migrations/*.pyc"  -delete
    2.删除当前数据库,或删除db.sqlite3它是否是您的情况。
    3.创建初始迁移并生成数据库模式:
    python manage.py makemigrations
    python manage.py migrate

    你很高兴。


    场景2:

    您希望清除所有迁移历史记录,但希望保留现有数据库。

    1.确保您的模型适合当前的数据库模式

    最简单的方法是尝试创建新的迁移:

    python manage.py makemigrations

    如果有任何挂起的迁移,请先应用它们。

    如果您看到以下消息:

    No changes detected

    你已准备好出发。

    2.清除每个应用的迁移历史记录

    现在,您需要按应用清除迁移历史记录应用。

    首先运行showmigrations命令,以便我们可以跟踪发生的情况:

    $ 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
    contenttypes
     [X] 0001_initial
     [X] 0002_remove_content_type_name
    core
     [X] 0001_initial
     [X] 0002_remove_mymodel_i
     [X] 0003_mymodel_bio
    sessions
     [X] 0001_initial

    清除迁移历史记录(请注意核心是我的应用程序的名称):

    $ python manage.py migrate --fake core zero

    结果将是这样的:

    Operations to perform:
      Unapply all migrations: core
    Running migrations:
      Rendering model states... DONE
      Unapplying core.0003_mymodel_bio... FAKED
      Unapplying core.0002_remove_mymodel_i... FAKED
      Unapplying core.0001_initial... FAKED

    现在showmigrations再次运行命令

    $ 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
    contenttypes
     [X] 0001_initial
     [X] 0002_remove_content_type_name
    core
     [ ] 0001_initial
     [ ] 0002_remove_mymodel_i
     [ ] 0003_mymodel_bio
    sessions
     [X] 0001_initial

    您必须为要重置迁移历史记录的所有应用程序执行此操作。

    3.删除实际的迁移文件。

    浏览每个项目应用程序迁移文件夹并删除内部的所有内容,__init__.py文件除外

    或者,如果您使用类似Unix的操作系统,则可以运行以下脚本(在项目目录中):

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

    PS:上面的示例将删除项目中的所有迁移文件。

    showmigrations再次运行

    $ 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
    contenttypes
     [X] 0001_initial
     [X] 0002_remove_content_type_name
    core
     (no migrations)
    sessions
     [X] 0001_initial
    4.创建初始迁移
    $ python manage.py makemigrations

    结果:

    Migrations for 'core':
      0001_initial.py:
        - Create model MyModel
    5.假初始迁移

    在这种情况下,您将无法应用初始迁移,因为数据库表已存在。我们想要做的是伪造这种迁移:

    $ python manage.py migrate --fake-initial
    python manage.py migrate --fake
     

    结果:

    Operations to perform:
      Apply all migrations: admin, core, contenttypes, auth, sessions
    Running migrations:
      Rendering model states... DONE
      Applying core.0001_initial... FAKED

    再跑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
    contenttypes
     [X] 0001_initial
     [X] 0002_remove_content_type_name
    core
     [X] 0001_initial
    sessions
     [X] 0001_initial

    我们都成立了:-)

     

  • 相关阅读:
    NLP(二十一)根据已有文本LSTM自动生成文本
    Keras(五)LSTM 长短期记忆模型 原理及实例
    Keras(六)Autoencoder 自编码 原理及实例 Save&reload 模型的保存和提取
    NLP(二十) 利用词向量实现高维词在二维空间的可视化
    Keras(四)CNN 卷积神经网络 RNN 循环神经网络 原理及实例
    NLP(十六) DL在NLP中的应用
    Keras(三)backend 兼容 Regressor 回归 Classifier 分类 原理及实例
    ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (60000, 28, 28)
    Keras(二)Application中五款已训练模型、VGG16框架解读
    Python实现 下载IJCAI会议所有论文
  • 原文地址:https://www.cnblogs.com/polly-ling/p/9407880.html
Copyright © 2011-2022 走看看