jcLee95 のブログ: https://blog.csdn.net/qq_28550263
この記事のアドレス: https://blog.csdn.net/qq_28550263/article/details/132862795
1. 説明
過去の移行履歴の状態が混乱しているため、python manage.py merge を実行しようとしたときに Django がデータベース テーブルを作成しようとすると問題が発生しました。エラー メッセージには、Django がモデルの ContentType を追跡するために使用するテーブルである django_content_type テーブルが欠落していることが記載されています。
一般的なエラー情報は次のとおりです。
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, users
Running migrations:
No migrations to apply.
Traceback (most recent call last):
File "C:\Python311\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 357, in execute
return Database.Cursor.execute(self, query, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: no such table: django_content_type
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\desktop\jcmusic\manage.py", line 22, in <module>
main()
File "D:\desktop\jcmusic\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Python311\Lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Python311\Lib\site-packages\django\core\management\__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python311\Lib\site-packages\django\core\management\base.py", line 402, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python311\Lib\site-packages\django\core\management\base.py", line 448, in execute
output = self.handle(*args, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\core\management\base.py", line 96, in wrapped
res = handle_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\core\management\commands\migrate.py", line 376, in handle
emit_post_migrate_signal(
File "C:\Python311\Lib\site-packages\django\core\management\sql.py", line 52, in emit_post_migrate_signal
models.signals.post_migrate.send(
File "C:\Python311\Lib\site-packages\django\dispatch\dispatcher.py", line 176, in send
return [
^
File "C:\Python311\Lib\site-packages\django\dispatch\dispatcher.py", line 177, in <listcomp>
(receiver, receiver(signal=self, sender=sender, **named))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\contrib\auth\management\__init__.py", line 51, in create_permissions
create_contenttypes(
File "C:\Python311\Lib\site-packages\django\contrib\contenttypes\management\__init__.py", line 127, in create_contenttypes
content_types, app_models = get_contenttypes_and_models(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\contrib\contenttypes\management\__init__.py", line 98, in get_contenttypes_and_models
content_types = {
^
File "C:\Python311\Lib\site-packages\django\db\models\query.py", line 394, in __iter__
self._fetch_all()
File "C:\Python311\Lib\site-packages\django\db\models\query.py", line 1867, in _fetch_all
self._result_cache = list(self._iterable_class(self))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\db\models\query.py", line 87, in __iter__
results = compiler.execute_sql(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\db\models\sql\compiler.py", line 1398, in execute_sql
cursor.execute(sql, params)
File "C:\Python311\Lib\site-packages\django\db\backends\utils.py", line 102, in execute
return super().execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\db\backends\utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "C:\Python311\Lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Python311\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 357, in execute
return Database.Cursor.execute(self, query, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: no such table: django_content_type
Django Shell を使用してデータベース テーブルをクリアすることにしました。プロセスを記録します。
2. 手順
重要なデータをバックアップする
クリーンアップする前に、さまざまな手段を使用してすべての重要なデータをバックアップしたことを確認してから、データベース内のすべてのテーブルをクリアします。
Django シェルに入る
python manage.py shell
スクリプトを入力してください
以下に記述したテーブル削除スクリプトを対話型シェルにコピーし、Enter キーを押して実行を開始します。
from django.db import connection
cursor = connection.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
for table in tables:
cursor.execute(f"DELETE FROM {
table[0]};")
次に、exit 関数を入力して Django シェル環境を終了します。
exit()
OK、これで移行とデータベース テーブルを再作成できます。
python manage.py makemigrations
python manage.py migrate