python - django.db.utils.IntegrityError : duplicate key value violates unique constraint "auth_permission_pkey" 错误

标签 python django django-migrations

卡住了 我有一个数据库,当我尝试使 python manage.py migrate 出现以下错误时:

django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_permission_pkey"
DETAIL:  Key (id)=(241) already exists.

以下是整个错误:

Operations to perform:
  Apply all migrations: admin, auth, companyapp, contenttypes, djcelery, kombu_transport_django, loginapp, projectmanagement, recruitmentproject, sessions, smallproject
Running migrations:
  No migrations to apply.
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 227, in handle
    self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/sql.py", line 53, in emit_post_migrate_signal
    **kwargs
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 193, in send
    for receiver in self._live_receivers(sender)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 83, in create_permissions
    Permission.objects.using(using).bulk_create(perms)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/db/models/query.py", line 443, in bulk_create
    ids = self._batched_insert(objs_without_pk, fields, batch_size)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/db/models/query.py", line 1080, in _batched_insert
    inserted_id = self._insert(item, fields=fields, using=self.db, return_id=True)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/db/models/query.py", line 1063, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
    cursor.execute(sql, params)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_permission_pkey"
DETAIL:  Key (id)=(241) already exists.

最佳答案

我已经尝试了上述答案,但这些都没有帮助我。

Django 内置命令来解决这个问题。

python manage.py sqlsequencereset auth | python manage.py dbshell

为什么会发生这种情况以及上述命令的作用的正确解释都可以在 this blog post 中找到。

关于python - django.db.utils.IntegrityError : duplicate key value violates unique constraint "auth_permission_pkey" 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46785841/

相关文章:

python - 如何将 pandas 系列中的 'NaN' 字符串转换为 dropna 的空值?

python - 如何将命名空间 url 添加到 django-rest-framework 路由器 View 集中

python - 添加/更改表单中的 ForeignKey 字段 - Django admin

python - Django:默认日期时间值导致嘈杂的测试

Django:如何从 ManyToMany 迁移到 ForeignKey?

python - 无法定义名称 "userInput"

python - 带有来自终端的参数的 bash 程序

python - 参数化 django 的迁移以跳过(--fake 以编程方式)

python - pyenv-virtualenv : Python and PIP not changed when activating/deactivating virtual environment 的问题

python - Jinja2 转储到一个对象中?