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

14

卡住了,我有一个数据库,在尝试运行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.

请发布导致问题的迁移内容。 - Adam Barnes
有两种可能的原因。也许迁移只添加了一个 unique=True 参数(rob 的回答已经足够),或者唯一字段是必需的(不允许 null=True)。在后一种(特殊)情况下,我可以写一个答案。 - hynekcer
类似的问题已经在 https://dev59.com/gmgu5IYBdhLWcg3wxZtM#11093322 得到解决。 - Mariah
4个回答

28

我尝试了上述答案,但它们并没有帮助我。

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

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

为什么会出现这种情况以及上面的命令是如何操作的,可以在这篇博客文章中找到详细解释。


1
谢谢。不知道那个命令存在。运行它,立即得到修复。谢谢。 - KhoPhi
1
是的!在SO上有很多针对个别位的修复方法,但这个修复方法对我很有效。 - Tom

5

我首先运行了以下命令来查看最新的序列号(对我而言是80),然后将其设置为更大的值,从而解决了这个问题:

SELECT last_value FROM auth_permission_id_seq;

ALTER SEQUENCE auth_permission_id_seq RESTART WITH 100;


帮助我的是将序列重置为 last_value + 1 - Codebender
对我来说有效 - undefined

4

您需要重置 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;

当我将生产数据库(仅数据)的转储还原到开发环境时,我本人也遇到了同样的问题。


0

没有太多其他的上下文,看起来你已经给你的模型添加了一个唯一约束,但是你的数据库中有违反此约束的行,所以迁移失败了。因此,在你的数据库中,你有两行数据,其中 auth_permission_pkey == 241

你需要删除或更改这一行,使其成为唯一的,并重新运行你的迁移。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接