解决Django migrate: Your models in app(s): ‘Mymodelclassname‘ have changes that are not yet reflected
這是一個“小”問題,但稍不注意會很令人困擾。在Django項目根目錄下執行命令
$python manage.py makemigrations沒有異常信息,但是在最終的migrate操作時出現錯誤提示(紅色):
$python manage.py migrateRunning migrations:
? No migrations to apply.
? Your models in app(s): 'Mymodelclassname' have changes that are not yet reflected in a migration, and so won't be applied.
? Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
刪除所有相關緩存,包括 __pycache__和migrations文件夾里的關聯文件。重新執行以上命令,結果出現另外的錯誤提示(灰色):
Running migrations:
? Applying Myappname.0002_alter_mymodelclassname_updated...Traceback (most recent call last):
? File "***/django/db/backends/utils.py", line 84, in _execute
??? return self.cursor.execute(sql, params)
? File "***/django/db/backends/sqlite3/base.py", line 423, in execute
??? return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: Myappname_mymodelclassname
?The above exception was the direct cause of the following exception:
Traceback (most recent call last):
? File "manage.py", line 22, in <module>
??? main()
? File "manage.py", line 18, in main
??? execute_from_command_line(sys.argv)
...
django.db.utils.OperationalError: no such table: Myappname_mymodelclassname
?Myappname和mymodelclassname確認沒有問題。猜測很可能是多次修改Myappname中的models.py文件,且反復執行makemigrations和migrate操作,導致Django緩存不一致。上述系列操作中,已確認刪除所有__pycache__緩存和migrations緩存。似乎“找茬”游戲中陷入僵局一樣。
但是,還有唯一一個存在數據庫緩存的,就是數據庫文件本身——db.sqlite3。在動手處理數據庫文件前,首先記得備份,將該文件重命名為: db.sqlite4。重新執行makemigrations和migrate操作:
$python manage.py makemigrations $python manage.py migrate結果通過:
Running migrations:
...
Applying sessions.0001_initial... OK
問題解決了!剩下的就是空數據庫和轉移舊數據庫的問題。
總結
以上是生活随笔為你收集整理的解决Django migrate: Your models in app(s): ‘Mymodelclassname‘ have changes that are not yet reflected的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 详解如何使用 PCem 安装 Windo
- 下一篇: AgentWeb url监听问题