欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django重置migrations文件的方法步驟

 更新時間:2019年05月01日 08:35:19   作者:破曉技術(shù)部落  
這篇文章主要介紹了Django重置migrations文件的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Django開發(fā)過程中如果數(shù)據(jù)庫變動過多導致migrations的文件越來越多,管理起來很不方便, 幸運的是Django提供了一種方式可以是這些文件重置到0001狀態(tài),而且不刪除原有數(shù)據(jù)。

確認migration文件跟數(shù)據(jù)庫同步

$ python3 manage.py makemigrations

如果提示 No changes detected 那么數(shù)據(jù)就是同步的。

查看當前migration文件記錄

$ python3 manage.py showmigrations
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

重置文件

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

刪除migrations的處init.py的其他文件

重新生產(chǎn)migrate文件

$ python3 manage.py makemigrations

同步到數(shù)據(jù)庫

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

fake是假冒偽裝的意思。執(zhí)行過程,但不應用數(shù)據(jù)。

這些就清爽多了。

場景一

不考慮數(shù)據(jù)庫數(shù)據(jù),可以完全清空數(shù)據(jù)庫。

步驟:

刪除所有migrations

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

刪除數(shù)據(jù)庫

重新生成migrations

python manage.py makemigrations
python manage.py migrate

場景二

有時候我們會直接導入完整的數(shù)據(jù)庫,包括數(shù)據(jù),這種情況下就不能簡單的清空數(shù)據(jù)庫。

這時我們的目的就是:清空數(shù)據(jù)庫的migration history,保證以后的migrate能正常使用,但要保留其他數(shù)據(jù)。

步驟:

從數(shù)據(jù)庫中刪除所有非0001_initial的migration history

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

使用migrate命令回滾0001_initial的migration history

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

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

python manage.py makemigrations

在數(shù)據(jù)庫中生成新的0001_initial記錄

python migrate --fake-initial

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python變量及數(shù)據(jù)類型用法原理匯總

    Python變量及數(shù)據(jù)類型用法原理匯總

    這篇文章主要介紹了Python變量及數(shù)據(jù)類型用法原理匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • Python模擬百度自動輸入搜索功能的實例

    Python模擬百度自動輸入搜索功能的實例

    今天小編就為大家分享一篇Python模擬百度自動輸入搜索功能的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • python多進程登錄遠端服務器

    python多進程登錄遠端服務器

    這篇文章主要介紹了python多進程登錄遠端服務器,文章應用實例簡易的方式詳細講解python多進程登錄遠端服務器的相關(guān)資料,需要的朋友可以參考以下文章的具體內(nèi)容
    2021-10-10
  • python 3.74 運行import numpy as np 報錯lib\site-packages\numpy\__init__.py

    python 3.74 運行import numpy as np 報錯lib\site-packages\numpy\_

    這篇文章主要介紹了python 3.74 運行import numpy as np 報錯lib\site-packages\numpy\__init__.py,原來需要更新一下numpy即可
    2019-10-10
  • 詳解python 破解網(wǎng)站反爬蟲的兩種簡單方法

    詳解python 破解網(wǎng)站反爬蟲的兩種簡單方法

    這篇文章主要介紹了詳解python 破解網(wǎng)站反爬蟲的兩種簡單方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • Python編程中*args與**kwargs區(qū)別作用詳解

    Python編程中*args與**kwargs區(qū)別作用詳解

    這篇文章主要介紹了Python編程中*args與**kwargs區(qū)別作用詳解
    2021-10-10
  • Python實現(xiàn)倉庫管理系統(tǒng)

    Python實現(xiàn)倉庫管理系統(tǒng)

    這篇文章主要為大家詳細介紹了Python實現(xiàn)倉庫管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • python 串口讀取+存儲+輸出處理實例

    python 串口讀取+存儲+輸出處理實例

    今天小編就為大家分享一篇python 串口讀取+存儲+輸出處理實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python?pygame繪制游戲圖像

    Python?pygame繪制游戲圖像

    這篇文章主要介紹了Python?pygame繪制游戲圖像,文章圍繞主題展開pygame模塊完成飛機大戰(zhàn)游戲的實戰(zhàn)開發(fā)的案例詳情,需要的朋友可以參考一下
    2022-08-08
  • Python利用fastapi實現(xiàn)上傳文件

    Python利用fastapi實現(xiàn)上傳文件

    FastAPI是一個現(xiàn)代的,快速(高性能)python?web框架。本文將利用fastapi實現(xiàn)上傳文件功能,文中的示例代碼講解詳細,需要的可以參考一下
    2022-06-06

最新評論