2011-09-06 110 views
3

我試圖清除我的數據庫,mydb,並重新填充以查看Django模型的某些更改的效果。然後發生這一切。我回滾到我的早期模型設置,但我仍然得到的錯誤。IntegrityError:(1062,重複輸入密鑰)

我不是MySQL專業人員,我不能完全弄清楚這個問題以及如何處理它;我嘗試用另一個名字創建一個新的數據庫,但那並沒有發生。我認爲這是我的Django項目創建的問題。

這裏是所有的信息:

 

C:\Users...>python manage.py syncdb 
Creating tables ... 
Creating table auth_permission 
Creating table auth_group_permissions 
Creating table auth_group 
Creating table auth_user_user_permissions 
Creating table auth_user_groups 
Creating table auth_user 
Creating table auth_message 
Creating table django_content_type 
Creating table django_session 
Creating table django_site 
Creating table django_admin_log 
Creating table forum_category 
Creating table forum_thread 

Creating table forum_post 

You just installed Django's auth system, which means you don't have any superusers defined. 
Would you like to create one now? (yes/no): yes 
Username (Leave blank to use 'me'): admin 
E-mail address: [email protected] 
Password: 
Password (again): 
Superuser created successfully. 
Traceback (most recent call last): 
    File "manage.py", line 14, in 
    execute_manager(settings) 
    File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 438, in execute_manager utility.execute() 
    File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 379, in execute self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "C:\Python27\lib\site-packages\django\core\management\base.py", line 191, in run_from_argv self.execute(*args, **options.__dict__) 
    File "C:\Python27\lib\site-packages\django\core\management\base.py", line 220, in execute output = self.handle(*args, **options) 
    File "C:\Python27\lib\site-packages\django\core\management\base.py", line 351, in handle return self.handle_noargs(**options) 
    File "C:\Python27\lib\site-packages\django\core\management\commands\syncdb.py", line 109, in handle_noargs emit_post_sync_signal(created_models, verbosity, interactive, db) 
    File "C:\Python27\lib\site-packages\django\core\management\sql.py", line 190, in emit_post_sync_signal interactive=interactive, db=db) 
    File "C:\Python27\lib\site-packages\django\dispatch\dispatcher.py", line 172, in send response = receiver(signal=self, sender=sender, **named) 
    File "C:\Python27\lib\site-packages\django\contrib\auth\management\__init__.p ", line 51, in create_permissions content_type=ctype 
    File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 138, in create return self.get_query_set().create(**kwargs) 
    File "C:\Python27\lib\site-packages\django\db\models\query.py", line 360, in create obj.save(force_insert=True, using=self.db) 
    File "C:\Python27\lib\site-packages\django\db\models\base.py", line 460, in save self.save_base(using=using, force_insert=force_insert, force_update=force_up 
date) 
    File "C:\Python27\lib\site-packages\django\db\models\base.py", line 553, in save_base result = manager._insert(values, return_id=update_pk, using=using) 
    File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 195, in _insert return insert_query(self.model, values, **kwargs) 
    File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1436, in insert_query return query.get_compiler(using=using).execute_sql(return_id) 
    File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 791, in execute_sql cursor = super(SQLInsertCompiler, self).execute_sql(None) 
    File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 735, in execute_sql cursor.execute(sql, params) 
    File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 34, in execute return self.cursor.execute(sql, params) 
    File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 86, in execute return self.cursor.execute(query, args) 
    File "C:\Users\me\AppData\Roaming\Python\Python27\site-packages\MySQLdb\cursors.py", line 174, in execute self.errorhandler(self, exc, value) 
    File "C:\Users\me\AppData\Roaming\Python\Python27\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue 

django.db.utils.IntegrityError: (1062, "Duplicate entry '9-delete_category' for key 'content_type_id'") 

 
#models.py 

from django.db import models 
from django.contrib.auth.models import User 

class Category(models.Model): 
    title = models.CharField(max_length=80) 

    class Meta: 
     verbose_name_plural = "categories" 
     permissions = (
      ("create_category", "Can create new categories"), 
      ("edit_category", "Can edit the titles of categories"), 
      ("delete_category", "Can delete a category"), 
      ("merge_category", "Can merge multiple categories together"), 
     ) 

class Thread(models.Model): 
    creation_date  = models.DateTimeField() 
    author    = models.ForeignKey(User) 
    title    = models.CharField(max_length=80) 
    category   = models.ForeignKey(Category) 

    class Meta: 
     ordering = ["-creation_date"] 
     permissions = (
      ("create_thread",   "Create new threads"), 
      ("edit_thread",    "Edit thread titles"), 
      ("delete_thread",   "Delete threads"), 
      ("merge_thread",   "Merge multiple threads together"), 
      ("lock_thread",    "Lock threads"), 
      ("unlock_thread",   "Open locked threads"), 
      ("ban_user_in_thread",  "Ban user from post in thread"), 
      ("timeout_user_in_thread", "Ban user from posting in thread temporarily"), 
      ("appoint_threadmin",  "Give a user mod-like permissions in a thread"), 
     ) 

class Bookmark(models.Model): 
    user   = models.ForeignKey(User) 
    thread   = models.ForeignKey(Thread) 

class Subscription(models.Model): 
    user   = models.ForeignKey(User) 
    thread   = models.ForeignKey(Thread) 

class Post(models.Model): 
    creation_date = models.DateTimeField() 
    author   = models.ForeignKey(User) 
    thread   = models.ForeignKey(Thread) 
    content   = models.TextField() 

    class Meta: 
     ordering = ["creation_date"] 
     permissions = (
      ("create_post", "Can create new post"), 
      ("edit_post", "Can edit all users' posts"), 
      ("delete_post", "Can delete posts"), 
     ) 

 
+----------------------------+ 
| Tables_in_mydb    | 
+----------------------------+ 
| auth_group     | 
| auth_group_permissions  | 
| auth_message    | 
| auth_permission   | 
| auth_user     | 
| auth_user_groups   | 
| auth_user_user_permissions | 
| django_admin_log   | 
| django_content_type  | 
| django_session    | 
| django_site    | 
| forum_bookmark    | 
| forum_category    | 
| forum_post     | 
| forum_subscription   | 
| forum_thread    | 
+----------------------------+ 

mysql> select * from django_content_type; 
+----+--------------+--------------+--------------+ 
| id | name   | app_label | model  | 
+----+--------------+--------------+--------------+ 
| 1 | permission | auth   | permission | 
| 2 | group  | auth   | group  | 
| 3 | user   | auth   | user   | 
| 4 | message  | auth   | message  | 
| 5 | content type | contenttypes | contenttype | 
| 6 | session  | sessions  | session  | 
| 7 | site   | sites  | site   | 
| 8 | log entry | admin  | logentry  | 
| 9 | category  | forum  | category  | 
| 10 | thread  | forum  | thread  | 
| 11 | bookmark  | forum  | bookmark  | 
| 12 | subscription | forum  | subscription | 
| 13 | post   | forum  | post   | 
+----+--------------+--------------+--------------+ 
+0

這聽起來像一個外國密鑰檢查失敗。 content_type_id在哪裏被用作外鍵? – Jrod

+1

你可以在第86行之前編輯'C:\ Python27 \ lib \ site-packages \ django \ db \ backends \ mysql \ base.py',添加'print query%args',然後重新運行syncdb以查看確切的查詢在失敗之前運行。 '9-delete_category'值當然不適合'content_type_id'字段。 –

回答

7

Django的自動creates few default permissions對每一個模型,它們是:adddeletechange。您正在嘗試使用相同的名稱創建權限,因此會出現完整性錯誤。從你的元描述中刪除delete_***,一切都應該沒問題。

+0

就是這樣。謝謝。 (當然還有其他人。) – Kiwi

0

接受的答案解決了原始問題,對於其他人來說,尋找特定錯誤是一個很好的起點。

要添加到這個以供將來參考,如果這不是具體的權限,但一般的燈具的加載順序問題的問題,這可以使用call_command解決:

from django.test import TestCase 
from django.core.management import call_command 


class Tests(TestCase): 
    @classmethod 
    def setUpTestData(cls): 
     # do some early data setup 
     ... 
     # then load data 
     call_command('loaddata', 'myfixture', verbosity=0) 

    def mytest(self): 
     # some tests in here 
     ... 
+1

這是一個完美的解決方案。請注意,setUpTestData在Django 1.8之後可用https://docs.djangoproject.com/en/1.8/topics/testing/tools/#django.test.TestCase.setUpTestData – electropoet