#31531: Loaddata fixtures with UUID as primary key
-----------------------------------------+------------------------
               Reporter:  max13fr        |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Uncategorized  |        Version:  3.0
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 Hello,

 I have an issue with Django 3. I use an UUID field as primary key. When
 I'm reloading fixtures, I got the error "UNIQUE constraint failed:
 api_test.id".

 It's seem that loaddata not detecting if the primary key already existing
 in DB so instead of an update, it tries to insert again the line with the
 same primary key.
 I'm wondering if the issue doesn't come from the lack of dash in the
 database (sqlite) : "724ea59910a249a1be2fc1fcb04d003e" instead of
 "724ea599-10a2-49a1-be2f-c1fcb04d003e".

 Important note : I converted the default id field to UUID with in a
 specific migration (but the table was empty), I don't know if it can also
 be the source of the issue.

 Here the example:

 api/models.py :

 {{{
 class Model Test(models.Model):
     id = models.UUIDField(primary_key=True, default=uuid.uuid4,
 editable=False)
     name = models.CharField(max_length=100)
 }}}

 api/fixtures/initial_data.json :

 {{{
 {
     "model": "api.Test",
     "pk": "724ea599-10a2-49a1-be2f-c1fcb04d003e",
     "fields": {
         "name": "Test 1"
     }
 }, {
     "model": "api.Test",
     "pk": "6bcb72f0-099b-416d-8b2c-fcd0bef76cbf",
     "fields": {
         "name": "Test 2"
     }
 }
 }}}

 {{{
 python manage.py loaddata initial_data
 }}}

 {{{
 django.db.utils.IntegrityError: Problem installing fixture
 'api/fixtures/initial_data.json': Could not load
 api.Test(pk=724ea599-10a2-49a1-be2f-c1fcb04d003e): UNIQUE constraint
 failed: api_test.id
 }}}

 Thanks in advance
 Max

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31531>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.e11028c1ecc3a2ffaab005e04ccf4063%40djangoproject.com.

Reply via email to