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

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

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


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. 步骤

备份重要数据

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

进入 Django Shell

python manage.py shell

输入脚本

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

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环境。

exit()

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

python manage.py makemigrations
python manage.py migrate


目录
相关文章
|
4月前
|
存储 关系型数据库 MySQL
Python Django框架下将MySQL数据库的内容在网页上动态展示(修订版-2021-05-17)
Python Django框架下将MySQL数据库的内容在网页上动态展示(修订版-2021-05-17)
30537 0
|
3月前
|
前端开发 数据库 Python
使用 Python 的 Web 框架(如 Django 或 Flask)来建立后端接口,用于处理用户的请求,从数据库中查找答案并返回给前端界面
【1月更文挑战第13天】使用 Python 的 Web 框架(如 Django 或 Flask)来建立后端接口,用于处理用户的请求,从数据库中查找答案并返回给前端界面
89 7
|
12天前
|
关系型数据库 API 数据库
Django中的数据库事务管理:确保数据一致性
【4月更文挑战第15天】Django框架提供强大的数据库事务管理,确保ACID属性,保证数据一致性和完整性。文章深入讨论了Django事务管理,包括使用`@transaction.atomic`装饰器和`transaction.atomic()`上下文管理器手动控制事务,以及低级API进行精细管理。注意避免长时间事务、选择合适隔离级别、正确处理异常及了解数据库特性。掌握这些技巧对构建可靠Web应用至关重要。
|
4月前
|
SQL 数据库 开发者
Python Web 开发: 什么是 Django ORM?如何使用它进行数据库操作?
Python Web 开发: 什么是 Django ORM?如何使用它进行数据库操作?
|
15天前
|
关系型数据库 MySQL 数据库连接
Django(四):Django项目部署数据库及服务器配置详解(MySQL)
Django(四):Django项目部署数据库及服务器配置详解(MySQL)
46 11
|
1月前
|
Shell Linux 数据库
【Shell 命令集合 网络通讯 】Linux 更新邮件别名数据库 newaliases命令 使用指南
【Shell 命令集合 网络通讯 】Linux 更新邮件别名数据库 newaliases命令 使用指南
29 1
|
1月前
|
监控 Shell Linux
【Shell 命令集合 磁盘管理 】Linux 检查和创建磁盘配额数据库 quotacheck命令使用教程
【Shell 命令集合 磁盘管理 】Linux 检查和创建磁盘配额数据库 quotacheck命令使用教程
32 0
|
1月前
|
存储 Shell Linux
【Shell 命令集合 文件管理】Linux 更新locate命令所使用的数据库 updatedb命令解析
【Shell 命令集合 文件管理】Linux 更新locate命令所使用的数据库 updatedb命令解析
155 0
|
1月前
|
Java 关系型数据库 MySQL
Java调用shell脚本实现数据库备份功能
本篇文章主要介绍怎样使用Java程序,执行服务器上的数据库备份Shell脚本进行MySQL数据库的备份功能。
|
1月前
|
存储 SQL 数据库
django如何连接sqlite数据库?
django如何连接sqlite数据库?
51 0