2017-10-17 118 views
2

陷我有,當我試圖讓python manage.py migrate它的如下給這個錯誤的數據庫:django.db.utils.IntegrityError:重複鍵值違反唯一約束「auth_permission_pkey」

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

請發佈導致問題的遷移內容。 –

+0

有兩種可能的原因。也許這個遷移只添加了一個'unique = True'參數(rob的答案已經足夠了),或者唯一的字段是必需的。 (不允許'null = True')我可以寫一個答案在後者(特殊)情況下。 – hynekcer

+0

已解決類似問題https://stackoverflow.com/a/11093322/5378183 – Mariah

回答

0

沒有太多的其他方面,看起來你已經添加了一個唯一約束到模型,但你必須在你的數據庫違反此約束行,因此遷移失敗。所以,在你的數據庫中,你有兩行auth_permission_pkey == 241

您需要刪除或更改此行,以便它是唯一的,並重新運行您的遷移。

相關問題