Fixed #36702 -- Made bulk_create() return pk values set by an expression.

This commit is contained in:
us77ipis
2025-11-06 16:58:01 +00:00
committed by GitHub
parent 125b63ca74
commit 6d4d99b3ce
2 changed files with 10 additions and 2 deletions

View File

@@ -842,7 +842,6 @@ class QuerySet(AltersData):
)
for obj_with_pk, results in zip(objs_with_pk, returned_columns):
for result, field in zip(results, opts.db_returning_fields):
if field != opts.pk:
setattr(obj_with_pk, field.attname, result)
for obj_with_pk in objs_with_pk:
obj_with_pk._state.adding = False

View File

@@ -884,6 +884,15 @@ class BulkCreateTests(TestCase):
(obj,) = DbDefaultPrimaryKey.objects.bulk_create([DbDefaultPrimaryKey()])
self.assertIsInstance(obj.id, datetime)
@skipUnlessDBFeature(
"can_return_rows_from_bulk_insert", "supports_expression_defaults"
)
def test_db_expression_primary_key(self):
(obj,) = DbDefaultPrimaryKey.objects.bulk_create(
[DbDefaultPrimaryKey(id=Now())]
)
self.assertIsInstance(obj.id, datetime)
@skipUnlessDBFeature("supports_transactions", "has_bulk_insert")
class BulkCreateTransactionTests(TransactionTestCase):