﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36260	bulk_create() does not fetch primary keys if they are generated by database	Dmitry Shachnev	Dmitry Shachnev	"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'>

----------------------------------------------------------------------
}}}"	Bug	closed	Database layer (models, ORM)	5.2	Normal	fixed			Ready for checkin	1	0	0	0	0	0
