卡住我有一个数据库,当我尝试制作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.
答案 0 :(得分:2)
我已经尝试了以上答案,但是这些并没有帮助我。
Django已内置命令来解决此问题。
中可以找到发生这种情况的原因以及上述命令的正确解释。python manage.py sqlsequencereset auth | python manage.py dbshell
答案 1 :(得分:0)
如果没有太多其他上下文,看起来您已经为模型添加了一个唯一约束,但是数据库中的行违反了此约束,因此迁移失败。因此,在您的数据库中,您有两行auth_permission_pkey == 241
。
您需要删除或更改此行,使其唯一,然后重新运行迁移。
答案 2 :(得分:0)
我先运行即可解决
从auth_permission_id_seq中选择last_value;
查看最新的序列号是什么(对我来说是80),然后将其设置为更大的值:
ALTER SEQUENCE auth_permission_id_seq重新启动为100;
答案 3 :(得分:0)
您必须重置auth_permission_id_seq
,因为它很可能低于最大值id
SELECT MAX(id)+1 FROM auth_permission
ALTER SEQUENCE auth_permission_id_seq RESTART WITH <result of previous cmd>;
其中100
是MAX(id)+1
。可能有一种方法可以在一个命令中执行此操作,但是我的SQL知识是有限的。
以下内容将向您显示序列号的当前值,而不是您必须设置的值(因为它可能与MAX(id)
中的auth_permission
相距很远)
SELECT last_value FROM auth_permission_id_seq;
当我将生产数据库的转储(仅数据)恢复到开发数据库时,我个人遇到相同的问题。