在Django中刷新数据库



我在django中设置了一个数据库,同时试图刷新数据库,我得到以下错误:

You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the "/home/luc/projects/PySorge/API/db.sqlite3" database,
and return each table to an empty state.
Are you sure you want to do this?

Type yes to continue, or no to cancel: yes
Traceback (most recent call last):
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: django_content_type.name

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/luc/projects/PySorge/API/manage.py", line 22, in <module>
main()
File "/home/luc/projects/PySorge/API/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/commands/flush.py", line 80, in handle
emit_post_migrate_signal(verbosity, interactive, database)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/sql.py", line 48, in emit_post_migrate_signal
models.signals.post_migrate.send(
File "/home/luc/.local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in send
return [
File "/home/luc/.local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 178, in <listcomp>
(receiver, receiver(signal=self, sender=sender, **named))
File "/home/luc/.local/lib/python3.9/site-packages/django/contrib/auth/management/__init__.py", line 42, in create_permissions
create_contenttypes(app_config, verbosity=verbosity, interactive=interactive, using=using, apps=apps, **kwargs)
File "/home/luc/.local/lib/python3.9/site-packages/django/contrib/contenttypes/management/__init__.py", line 132, in create_contenttypes
ContentType.objects.using(using).bulk_create(cts)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/query.py", line 506, in bulk_create
returned_columns = self._batched_insert(
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/query.py", line 1277, in _batched_insert
self._insert(item, fields=fields, using=self.db, ignore_conflicts=ignore_conflicts)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/query.py", line 1254, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1397, in execute_sql
cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: django_content_type.name

尝试迁移时,发生以下错误:

Operations to perform:
Apply all migrations: admin, api, auth, contenttypes, sessions
Running migrations:
Applying api.0001_initial...Traceback (most recent call last):
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 411, in execute
return Database.Cursor.execute(self, query)
sqlite3.OperationalError: duplicate column name: ID

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/luc/projects/PySorge/API/manage.py", line 22, in <module>
main()
File "/home/luc/projects/PySorge/API/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/home/luc/.local/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 243, in handle
post_migrate_state = executor.migrate(
File "/home/luc/.local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 324, in create_model
self.execute(sql, params or None)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
File "/home/luc/.local/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 411, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: duplicate column name: ID

我使用的django版本是3.1.6。我不太确定如何解决这个问题,因为我不太熟悉数据库。似乎错误是由重复的字段id引起的-我当前的数据库设置确实有一个名为id的字段。

我的建议,虽然肯定不是最好的方法,是删除数据库(删除项目中实际的sqlite3文件),然后删除所有以前的迁移,然后再尝试进行迁移。

我有一个非常老的Django站点(在tree中仍然有南迁移!),它一直在SQLite数据库上运行。在某些时候,Django从django_content_types表中删除了name列。模型现在使用基于反射模型类的属性。我不知道为什么这没有被Django迁移捕捉到,但是我已经知道在让某些东西运行的血雾中进行迁移,无论如何。

我刚刚删除了./manage.py dbshell中的列:

ALTER TABLE django_content_type DROP COLUMN name;

并且迁移再次工作。没有感觉到不良影响。我所有的旧内容类型都在里面,一切都还正常。

python manage.py reset_db

这将删除所有表。如果您想要完全清除

,它还有助于删除项目中的所有迁移文件。

最新更新