Opened 4 days ago
Last modified 3 days ago
#36260 assigned Bug
bulk_create() does not fetch primary keys if they are generated by database
Description ¶
In our project, we use a function that generates UUID on database side (it’s uuid7
function from PostgreSQL 18).
The bulk_create
function uses _is_pk_set()
method to check if the object already has a primary key assigned. However, _is_pk_set()
wrongly returns True in this case: primary key is not None, however it is a DatabaseDefault
instance and does not actually return a value.
Because of this, the objects returned by bulk_create
do not actually have an ID.
I prepared a patch to fix this, and wrote a test case for it. Without my patch, the test fails with the following error:
====================================================================== FAIL: test_db_default_primary_key (bulk_create.tests.BulkCreateTests.test_db_default_primary_key) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/dmitry/upstream/django/tests/bulk_create/tests.py", line 878, in test_db_default_primary_key self.assertIsInstance(obj1.id, UUID) ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^ AssertionError: <django.db.models.expressions.DatabaseDefault object at 0x7f2563f6b570> is not an instance of <class 'uuid.UUID'> ----------------------------------------------------------------------
According to the ticket's flags, the next step(s) to move this issue forward are:
- For anyone except the patch author to review the patch using the patch review checklist and either mark the ticket as "Ready for checkin" if everything looks good, or leave comments for improvement and mark the ticket as "Patch needs improvement".
Change History (3)
comment:1 by , 3 days ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
comment:2 by , 3 days ago
Patch needs improvement: | set |
---|
comment:3 by , 3 days ago
Patch needs improvement: | unset |
---|
Patch LGTM. Left a comment about a potential improvement to object partitioning by primary key that could be done in a follow up optimization commit while we're around.
Not a release blocker as
db_default
was introduced in 5.0 and I could reproduce the issue against it.