Python django 數(shù)據(jù)庫遷移失敗
在執(zhí)行 python3 manage.py makemigrations 命令時,遇到了 mysql 數(shù)據(jù)表創(chuàng)建失敗的問題。以下是問題詳情:
- 操作系統(tǒng):Windows 10
- Python 版本:3.8.6
- Django 版本:3.2.19
- mysqlclient 版本:2.1.1
- MySQL 版本:0.0.3
項目的目錄結(jié)構、models.py 文件內(nèi)容,以及 settings.py 文件中的應用程序注冊和數(shù)據(jù)庫配置都已確認無誤。
然而,在運行 python3 manage.py makemigrations 和 Python3 manage.py migrate 命令后,MySQL 中的數(shù)據(jù)表仍未創(chuàng)建成功。
立即學習“Python免費學習筆記(深入)”;
解決方案
經(jīng)過分析,問題出在使用的 Python 解釋器上。執(zhí)行 python3 命令實際上指向的是 Windows 自帶的 Python 解釋器,而不是安裝包中安裝的 Python 解釋器。
解決步驟:
- 使用 python 命令(不帶版本號)來調(diào)用安裝包安裝的 Python 解釋器。
- 在 python 解釋器中,輸入 python manage.py makemigrations 和 python manage.py migrate 命令。此時,數(shù)據(jù)表將成功創(chuàng)建。