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

Reported by: Dmitry Shachnev Owned by: Dmitry Shachnev
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
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'>

----------------------------------------------------------------------

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 Simon Charette, 3 days ago

Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Not a release blocker as db_default was introduced in 5.0 and I could reproduce the issue against it.

comment:2 by Simon Charette, 3 days ago

Patch needs improvement: set

comment:3 by Simon Charette, 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.

Last edited 3 days ago by Simon Charette (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top