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.
Comment (by Mark Dawson):
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
--
Ticket URL: <https://code.djangoproject.com/ticket/31531#comment:1>
* cc: Mark Dawson (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/31531#comment:2>
* status: new => closed
* resolution: => fixed
Comment:
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
--
Ticket URL: <https://code.djangoproject.com/ticket/31531#comment:3>
* resolution: fixed => duplicate
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => Bug
Comment:
Duplicate of #31071.
--
Ticket URL: <https://code.djangoproject.com/ticket/31531#comment:4>