1
0
mirror of https://github.com/django/django.git synced 2025-10-29 00:26:07 +00:00

[5.2.x] Fixed #36502 -- Restored UNNEST strategy for foreign key bulk inserts on PostgreSQL.

Regression in 764af7a3d6.

Backport of 0fe218842e from main.
This commit is contained in:
Simon Charette
2025-07-10 12:33:00 -04:00
committed by Mariusz Felisiak
parent abc10ab7f9
commit 3df1ad57bf
3 changed files with 21 additions and 3 deletions

View File

@@ -1,10 +1,11 @@
import unittest
from datetime import date
from django.db import connection
from django.db.models.expressions import RawSQL
from django.test import TestCase
from ..models import Square
from ..models import Article, Reporter, Square
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL tests")
@@ -27,3 +28,17 @@ class BulkCreateUnnestTests(TestCase):
[Square(root=2, square=4), Square(root=3, square=9)]
)
self.assertIn("UNNEST", ctx[0]["sql"])
def test_unnest_eligible_foreign_keys(self):
reporter = Reporter.objects.create()
with self.assertNumQueries(1) as ctx:
articles = Article.objects.bulk_create(
[
Article(pub_date=date.today(), reporter=reporter),
Article(pub_date=date.today(), reporter=reporter),
]
)
self.assertIn("UNNEST", ctx[0]["sql"])
self.assertEqual(
[article.reporter for article in articles], [reporter, reporter]
)