首页 > 解决方案 > 在 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 的字段。

标签: django

解决方案


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


推荐阅读