简记:使用 Django Shell 清空 数据库表

简记 使用 Django Shell 清空所有数据库表


jcLee95的博客https://blog.csdn.net/qq_28550263

本文地址https://blog.csdn.net/qq_28550263/article/details/132862795


目 录



[1. 描述](#1. 描述)

由于历史的迁移历史的混乱状态导致尝试运行 python manage.py migrate 时,Django 试图创建数据库表时发生了问题。错误信息中提到了缺少 django_content_type 表,这是Django用于跟踪模型的ContentType的表。

错误大致信息如下:

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. 步骤](#2. 步骤)

备份重要数据

清理前,确保你使用各种手段备份过了所有重要数据,然后才清空数据库中的所有表。

进入 Django Shell

bash 复制代码
python manage.py shell

输入脚本

拷贝以下写好的删表脚本到交互式Shell中,键入回车开始执行。

py 复制代码
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 Shell环境。

bash 复制代码
exit()

可以了,现在可以重新创建迁移和数据库表了:

bash 复制代码
python manage.py makemigrations
python manage.py migrate
相关推荐
time never ceases20 分钟前
使用docker方式进行Oracle数据库的物理迁移(helowin/oracle_11g)
数据库·docker·oracle
Frank牛蛙23 分钟前
1.每日SQL----2024/11/7
数据库·sql
Ciderw25 分钟前
块存储、文件存储和对象存储详细介绍
网络·数据库·nvme·对象存储·存储·块存储·文件存储
薛晓刚26 分钟前
数据库优化指南:如何将基本功能运用到极致?
数据库
stars_User30 分钟前
MySQL数据库面试题(下)
数据库·mysql
未来之窗软件服务1 小时前
sql速度优化多条合并为一条语句
数据库
山东布谷科技官方1 小时前
布谷直播源码部署服务器关于数据库配置的详细说明
运维·服务器·数据库·直播系统源码·直播源码·直播系统搭建·直播软件开发
易云码1 小时前
信息安全建设方案,网络安全等保测评方案,等保技术解决方案,等保总体实施方案(Word原件)
数据库·物联网·安全·web安全·低代码
newxtc1 小时前
【客观理性深入讨论国产中间件及数据库-科创基础软件】
数据库·中间件·国产数据库·国产中间件·科创
水月梦镜花1 小时前
redis:list列表命令和内部编码
数据库·redis·list