[Django] #31531: Loaddata fixtures with UUID as primary key

19 views
Skip to first unread message

Django

unread,
May 2, 2020, 4:08:13 AM5/2/20
to django-...@googlegroups.com
#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.

Django

unread,
May 3, 2020, 12:25:53 AM5/3/20
to django-...@googlegroups.com
#31531: Loaddata fixtures with UUID as primary key
-------------------------------+--------------------------------------

Reporter: max13fr | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

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>

Django

unread,
May 3, 2020, 12:28:59 AM5/3/20
to django-...@googlegroups.com
#31531: Loaddata fixtures with UUID as primary key
-------------------------------+--------------------------------------

Reporter: max13fr | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Mark Dawson):

* cc: Mark Dawson (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/31531#comment:2>

Django

unread,
May 3, 2020, 4:52:45 AM5/3/20
to django-...@googlegroups.com
#31531: Loaddata fixtures with UUID as primary key
-------------------------------+--------------------------------------
Reporter: max13fr | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by max13fr):

* 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>

Django

unread,
May 4, 2020, 12:01:29 AM5/4/20
to django-...@googlegroups.com
#31531: Loaddata fixtures with UUID as primary key
-------------------------------------+-------------------------------------
Reporter: max13fr | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* 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>

Reply all
Reply to author
Forward
0 new messages