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

Piy*_*are 6 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.
Run Code Online (Sandbox Code Playgroud)

以下是整个错误:

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.
Run Code Online (Sandbox Code Playgroud)

jib*_*hew 14

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

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

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

关于为什么会发生这种情况以及上述命令的作用的正确解释都可以在这篇博文中找到


eli*_*eve 5

我通过首先运行解决了这个问题

从 auth_permission_id_seq 选择最后一个值;

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

更改序列 auth_permission_id_seq 以 100 重新启动;


nbe*_*hat 5

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

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

哪儿100MAX(id)+1。可能有一种方法可以通过一个命令来完成此操作,但我的 SQL 知识有限。

下面将向您显示序列号的当前值,而不是您必须将其设置为的值(因为它可能与 in 相差很MAX(id)auth_permission

SELECT last_value FROM auth_permission_id_seq;
Run Code Online (Sandbox Code Playgroud)

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