简记:使用 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


目录
相关文章
|
17天前
|
ARouter 关系型数据库 MySQL
Django项目同时连接多个不同的数据库(7)
【7月更文挑战第7天】在Django项目中配置连接多个数据库,你需要: 1. 在`settings.py`中配置多个数据库, 2. 在`settings.py`内设置数据库路由,指定每个应用使用的数据库,
34 2
|
19天前
|
关系型数据库 MySQL 数据库连接
Django如何修改项目名,修改数据库(5)
【7月更文挑战第5天】 ### 项目迁移与数据库更换步骤 1. 修改项目目录和配置文件名。 2. 更新`manage.py`中的模块名。 3. 全局替换`settings.py`中的旧项目名。 4. 删除旧路由配置。 5. 在`settings.py`中更新`DATABASES`,配置新的数据库连接(如MySQL)。 6. 从`INSTALLED_APPS`和可能的`DATABASE_APPS_MAPPING`中移除无用模块。
15 3
|
18天前
|
SQL 数据库 Python
Django框架数据库ORM查询操作(6)
【7月更文挑战第6天】```markdown Django ORM常用数据库操作:1) 查询所有数据2) 根据ID查询 3) 精确查询 4) 分页排序
25 1
|
20天前
|
数据库 Python
Django数据库类库MySQLdb使用详解
在Django中使用MySQLdb数据库类库,需要遵循以下步骤: 1. 安装MySQLdb:首先,需要在你的Python环境中安装MySQLdb类库。可以使用pip工具进行安装,命令如下:`pip install mysqlclient` 1. 配置Django数据库设置:在Django的settings.py文件中,需要配置DATABASES字典,用于指定使用MySQLdb以及数据库的相关信息。配置样例如下: ``` DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql',
21 3
|
1月前
|
关系型数据库 MySQL 数据库
Django与MySQL:配置数据库的详细步骤
Django与MySQL:配置数据库的详细步骤
|
1月前
|
JSON 缓存 数据库
Django ORM的QuerySet:解锁数据库交互的魔法钥匙
Django ORM的QuerySet:解锁数据库交互的魔法钥匙
|
1月前
|
缓存 运维 Serverless
Serverless 应用引擎产品使用合集之基于django应用模板创建的FC,如何配置数据库
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
|
1月前
|
数据库 Python
Django——数据库
Django——数据库
|
2月前
|
关系型数据库 MySQL 数据库连接
Django(四):Django项目部署数据库及服务器配置详解(MySQL)
Django(四):Django项目部署数据库及服务器配置详解(MySQL)
308 11
|
2月前
|
前端开发 API 数据库
Django(五):如何在Django中通过API提供数据库数据给前端
Django(五):如何在Django中通过API提供数据库数据给前端
116 9