diff options
| author | Simon Charette <charette.s@gmail.com> | 2025-07-10 12:33:00 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2025-07-10 18:36:01 +0200 |
| commit | 3df1ad57bfcabaf75e4617064d79610f185f3f9f (patch) | |
| tree | 0e792629761bce12f1347eb2dfc3bb01b56ad8c9 | |
| parent | abc10ab7f9ddc01807dd4afc33be21d3c302435d (diff) | |
[5.2.x] Fixed #36502 -- Restored UNNEST strategy for foreign key bulk inserts on PostgreSQL.
Regression in 764af7a3d6c0b543dcf659a2c327f214da768fe4.
Backport of 0fe218842e0e396e3ab3982bd21227968a9e7fd8 from main.
| -rw-r--r-- | django/db/backends/postgresql/compiler.py | 3 | ||||
| -rw-r--r-- | docs/releases/5.2.5.txt | 4 | ||||
| -rw-r--r-- | tests/backends/postgresql/test_compilation.py | 17 |
3 files changed, 21 insertions, 3 deletions
diff --git a/django/db/backends/postgresql/compiler.py b/django/db/backends/postgresql/compiler.py index c4080ac037..344773fb7a 100644 --- a/django/db/backends/postgresql/compiler.py +++ b/django/db/backends/postgresql/compiler.py @@ -40,7 +40,8 @@ class SQLInsertCompiler(BaseSQLInsertCompiler): # unnest'able (e.g. array and geometry types are known to be # problematic). or any( - field.get_internal_type() not in self.connection.data_types + (field.target_field if field.is_relation else field).get_internal_type() + not in self.connection.data_types for field in fields ) # Compilable cannot be combined in an array of literal values. diff --git a/docs/releases/5.2.5.txt b/docs/releases/5.2.5.txt index 9454c0a67d..a72a3fe078 100644 --- a/docs/releases/5.2.5.txt +++ b/docs/releases/5.2.5.txt @@ -9,4 +9,6 @@ Django 5.2.5 fixes several bugs in 5.2.4. Bugfixes ======== -* ... +* Fixed a regression in Django 5.2.1 that prevented the usage of ``UNNEST`` + PostgreSQL strategy of ``QuerySet.bulk_create()`` with foreign keys + (:ticket:`36502`). diff --git a/tests/backends/postgresql/test_compilation.py b/tests/backends/postgresql/test_compilation.py index 67fe893e35..11c4015e01 100644 --- a/tests/backends/postgresql/test_compilation.py +++ b/tests/backends/postgresql/test_compilation.py @@ -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] + ) |
