django.db.utils.IntegrityError:重复键值违反唯一约束“auth_permission_pkey”

Posted

技术标签:

【中文标题】django.db.utils.IntegrityError:重复键值违反唯一约束“auth_permission_pkey”【英文标题】:django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_permission_pkey" 【发布时间】:2018-03-28 21:23:17 【问题描述】:

卡住了我有一个数据库,当我尝试在其中创建 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.

【问题讨论】:

请发布导致问题的迁移内容。 有两种可能的原因。也许该迁移仅添加了unique=True 参数(rob 的答案就足够了)或者需要唯一字段。 (不允许null=True)我可以在后一种(特殊)情况下写一个答案。 类似问题已解决***.com/a/11093322/5378183 【参考方案1】:

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

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

python manage.py sqlsequencereset auth | python manage.py dbshel​​l

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

【讨论】:

谢谢。不知道这个命令存在。运行它,立即修复。谢谢。 是的!对于个别位有很多关于 SO 的修复,但这对我来说是固定的。【参考方案2】:

您必须重置auth_permission_id_seq,因为它很可能低于最大id

SELECT MAX(id)+1 FROM auth_permission
ALTER SEQUENCE auth_permission_id_seq RESTART WITH <result of previous cmd>;

其中100MAX(id)+1。可能有一种方法可以在一个命令中执行此操作,但我的 SQL 知识有限。

以下将显示序列号的当前值,而不是您必须将其设置为的值(因为它可能与auth_permission 中的MAX(id) 相去甚远)

SELECT last_value FROM auth_permission_id_seq;

当我将生产数据库的转储(仅数据)恢复到开发数据库时,我个人也遇到了同样的问题。

【讨论】:

【参考方案3】:

我通过第一次运行解决了这个问题

从 auth_permission_id_seq 中选择最后一个值;

查看最新的序列号是什么(对我来说是 80),然后我将其设置为更大的值:

ALTER SEQUENCE auth_permission_id_seq RESTART WITH 100;

【讨论】:

帮助我的是将序列重置为last_value + 1【参考方案4】:

在没有太多其他上下文的情况下,看起来您已向模型添加了唯一约束,但您的数据库中存在违反此约束的行,因此迁移失败。因此,在您的数据库中,您有两行 auth_permission_pkey == 241

您需要删除或更改此行以使其唯一,然后重新运行迁移。

【讨论】:

以上是关于django.db.utils.IntegrityError:重复键值违反唯一约束“auth_permission_pkey”的主要内容,如果未能解决你的问题,请参考以下文章