Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#31531 closed Bug (duplicate)

Loaddata fixtures with UUID as primary key

Reported by: max13fr Owned by: nobody
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords:
Cc: Mark Dawson Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (4)

comment:1 by Mark Dawson, 5 years ago

Hey Max,

I tried to reproduce your bug, but it worked fine for me. I have put my working code here https://github.com/markdawson/ticket_31531

comment:2 by Mark Dawson, 5 years ago

Cc: Mark Dawson added

comment:3 by max13fr, 5 years ago

Resolution: fixed
Status: newclosed

Hello,

Thanks for your help, effectively in your example it's working. I also tried to change id from default integer to uuid in a second migration but everything works fine.

After reviewing everything in my project, I finally found the issue : I was using Django 3.0 version (was thinking pip automatically add subversion but not).
So I changed to "django==3.0.*" that install current 3.0.5 version.

And now the loaddata is working fine !

Thanks for your time !

Max

comment:4 by Mariusz Felisiak, 5 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: fixedduplicate
Type: UncategorizedBug

Duplicate of #31071.

Note: See TracTickets for help on using tickets.
Back to Top