#36260: bulk_create() does not fetch primary keys if they are generated by database
-------------------------------------+-------------------------------------
Reporter: Dmitry | Owner: Dmitry Shachnev
Shachnev |
Type: | Status: assigned
Uncategorized |
Component: | Version: 5.2
Uncategorized |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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'>
----------------------------------------------------------------------
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/36260>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.