2010-09-28 13 views
6

मैं निम्न कमांड का इस्तेमाल किया 2 जुड़नार उत्पन्न करने के लिए:लोड करने में समस्या Django स्थिरता: IntegrityError: (1062, "कुंजी 'user_id' के लिए डुप्लिकेट प्रविष्टि '4'")

./manage.py dumpdata --format=json --indent=4 --natural auth.User > fixtures/user.json 
./manage.py dumpdata --format=json --indent=4 --natural --exclude=contenttypes --exclude=auth > fixtures/full.json 

मैं निम्नलिखित स्थिरता मिल गया है नामित user.json:

[ 
    { 
     "pk": 4, 
     "model": "auth.user", 
     "fields": { 
      "username": "foo", 
      "first_name": "Se\u00e1n", 
      "last_name": "Hayes", 
      "is_active": true, 
      "is_superuser": true, 
      "is_staff": true, 
      "last_login": "2010-09-27 21:57:45", 
      "groups": [], 
      "user_permissions": [], 
      "password": "!", 
      "email": "[email protected]", 
      "date_joined": "2010-09-27 21:57:45" 
     } 
    } 
] 

और निम्न स्थिरता नामित full.json:

[ 
    { 
     "pk": "72a75887b4a0ce06a61f9183fe1c0e15", 
     "model": "sessions.session", 
     "fields": { 
      "expire_date": "2010-10-11 21:57:45", 
      "session_data": "gAJ9cQEoVRJfYXV0aF91c2VyX2JhY2tlbmRxAlUOZmIuYXV0aC5GYkF1dGhxA1UNX2F1dGhfdXNl\ncl9pZHEEigEEdS5hOGZlODU0MmRjYmUwNmEzODIwNjhiYzYyODc2MWQxZA==\n" 
     } 
    }, 
    { 
     "pk": 1, 
     "model": "sites.site", 
     "fields": { 
      "domain": "example.com", 
      "name": "example.com" 
     } 
    }, 
    { 
     "pk": 2, 
     "model": "common.userprofile", 
     "fields": { 
      "money": 10, 
      "energy": 10, 
      "experience": 0, 
      "stamina": 10, 
      "health": 10, 
      "user": 4 
     } 
    }, 
    { 
     "pk": 2, 
     "model": "missions.missionprofile", 
     "fields": { 
      "user": 4, 
      "last_area_viewed": null 
     } 
    }, 
    { 
     "pk": 1, 
     "model": "fb.facebookuser", 
     "fields": { 
      "updated": "2010-09-27 21:57:45", 
      "uid": "24411841", 
      "created": "2010-09-27 21:57:45", 
      "access_token": "foo", 
      "url": "http://www.facebook.com/profile.php?id=24411841", 
      "user": 4, 
      "img_url": null, 
      "name": "Se\u00e1n Hayes" 
     } 
    } 
] 

निम्न कमांड चल रहा है (में या तो आदेश):

Problem installing fixture '/projectpath/fixtures/full.json': Traceback (most recent call last): 
    File "/usr/lib/pymodules/python2.6/django/core/management/commands/loaddata.py", line 169, in handle 
    obj.save(using=using) 
    File "/usr/lib/pymodules/python2.6/django/core/serializers/base.py", line 165, in save 
    models.Model.save_base(self.object, using=using, raw=True) 
    File "/usr/lib/pymodules/python2.6/django/db/models/base.py", line 528, in save_base 
    result = manager._insert(values, return_id=update_pk, using=using) 
    File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 195, in _insert 
    return insert_query(self.model, values, **kwargs) 
    File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 1479, in insert_query 
    return query.get_compiler(using=using).execute_sql(return_id) 
    File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line 783, in execute_sql 
    cursor = super(SQLInsertCompiler, self).execute_sql(None) 
    File "/usr/lib/pymodules/python2.6/django/db/models/sql/compiler.py", line 727, in execute_sql 
    cursor.execute(sql, params) 
    File "/usr/lib/pymodules/python2.6/django/db/backends/util.py", line 15, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/lib/pymodules/python2.6/django/db/backends/mysql/base.py", line 86, in execute 
    return self.cursor.execute(query, args) 
    File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute 
    self.errorhandler(self, exc, value) 
    File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler 
    raise errorclass, errorvalue 
IntegrityError: (1062, "Duplicate entry '4' for key 'user_id'") 

क्या हो रहा है पर कोई भी विचार:

./manage.py loaddata user 
./manage.py loaddata full 

निम्नलिखित अपवाद को जन्म देती है? मुझे पता है कि MySQL में आगे संदर्भों में कोई समस्या है, लेकिन इससे कोई फर्क नहीं पड़ता कि आईडी 4 के साथ उपयोगकर्ता ऑब्जेक्ट को उस फिक्स्चर से पहले इंस्टॉल किया जा रहा है जिसमें उसमें विदेशी कुंजी है, है ना?

+0

देखें मैं भी विभाजित full.json छोटे जुड़नार में करने की कोशिश की है और एक बार में एक स्थापित। जैसे ही उपयोगकर्ता के लिए एक विदेशी कुंजी मौजूद है, त्रुटि उत्पन्न होती है। –

+0

क्या आप यहां अपने मॉडल के लिए कोड पोस्ट कर सकते हैं? –

+1

क्या आपके पास अपने मॉडल पर कोई '' अद्वितीय 'सेटिंग सेट है? –

उत्तर

15

अशोक की टिप्पणी के अनुसार, जब मैं एक ही समस्या थी, यह "कच्चे" मोड जो जाहिरा तौर पर इसका मतलब है एक दृढ़ है में चल रहा है कि क्या जांच करने के लिए मेरी संकेत हैंडलर बदलकर हल किया गया था लोड किए जा रहे:

def create_user_profile(sender, instance, created, **kwargs): 
    if created and not kwargs.get('raw', False): 
     UserProfile.objects.create(user=instance) 

How do I prevent fixtures from conflicting with django post_save signal code?

0

Django की स्थिर स्थिरता एक एंटी-पटरन है, यही कारण है कि आपको इस तरह की समस्या है। मेरा सुझाव है कि आप कुछ पुस्तकालयों का उपयोग करें जो आपके लिए इस फिक्स्चर उत्पन्न करते हैं, जिसमें स्वाभाविक रूप से आईडी भी शामिल है।

मैं सुझाव है कि आप Dynamic Dynamic Fixture

संबंधित मुद्दे