#36260 closed Bug (fixed)
bulk_create() does not fetch primary keys if they are generated by database
| Reported by: | Dmitry Shachnev | Owned by: | Dmitry Shachnev |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 5.2 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
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'>
----------------------------------------------------------------------
Change History (7)
comment:1 by , 8 months ago
| Component: | Uncategorized → Database layer (models, ORM) |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
comment:2 by , 8 months ago
| Patch needs improvement: | set |
|---|
comment:3 by , 8 months ago
| Patch needs improvement: | unset |
|---|
Patch is 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.
comment:4 by , 8 months ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Not a release blocker as
db_defaultwas introduced in 5.0 and I could reproduce the issue against it.