diff options
| author | Simon Charette <charette.s@gmail.com> | 2025-01-17 13:41:22 -0500 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-01-20 14:15:32 +0100 |
| commit | 22fc151bb86a553d84c62d7effd289356e9b6c6c (patch) | |
| tree | 1c3e3de407b882885b79090f7fec5ebe0ef08c4a | |
| parent | f5772de69679efb54129ac1cbca3579b512778af (diff) | |
Fixed #36107 -- Adjusted UNNEST bulk_create strategy to opt-out sized arrays.
The array fields opt-out heuristic failed to account for sized arrays.
Note that we keep relying on db_type as opposed to performing an ArrayField
instance check against the column's field as there could be other
implementations of model fields that use Postgres arrays to store the
optimization must be disabled for all of them.
Refs #35936.
Thanks Claude Paroz for the report and test.
| -rw-r--r-- | django/db/backends/postgresql/compiler.py | 2 | ||||
| -rw-r--r-- | tests/postgres_tests/migrations/0002_create_test_models.py | 22 | ||||
| -rw-r--r-- | tests/postgres_tests/models.py | 4 | ||||
| -rw-r--r-- | tests/postgres_tests/test_array.py | 11 |
4 files changed, 38 insertions, 1 deletions
diff --git a/django/db/backends/postgresql/compiler.py b/django/db/backends/postgresql/compiler.py index 2394d90f55..3b972b5ba5 100644 --- a/django/db/backends/postgresql/compiler.py +++ b/django/db/backends/postgresql/compiler.py @@ -43,7 +43,7 @@ class SQLInsertCompiler(BaseSQLInsertCompiler): db_types = [field.db_type(self.connection) for field in fields] # Abort if any of the fields are arrays as UNNEST indiscriminately # flatten them instead of reducing their nesting by one. - if any(db_type.endswith("[]") for db_type in db_types): + if any(db_type.endswith("]") for db_type in db_types): return super().assemble_as_sql(fields, value_rows) return InsertUnnest(["(%%s)::%s[]" % db_type for db_type in db_types]), [ list(map(list, zip(*value_rows))) diff --git a/tests/postgres_tests/migrations/0002_create_test_models.py b/tests/postgres_tests/migrations/0002_create_test_models.py index 188f79607d..31705ae21a 100644 --- a/tests/postgres_tests/migrations/0002_create_test_models.py +++ b/tests/postgres_tests/migrations/0002_create_test_models.py @@ -168,6 +168,28 @@ class Migration(migrations.Migration): bases=(models.Model,), ), migrations.CreateModel( + name="WithSizeArrayModel", + fields=[ + ( + "id", + models.AutoField( + verbose_name="ID", + serialize=False, + auto_created=True, + primary_key=True, + ), + ), + ( + "field", + ArrayField(models.FloatField(), size=2, null=True, blank=True), + ), + ], + options={ + "required_db_vendor": "postgresql", + }, + bases=(models.Model,), + ), + migrations.CreateModel( name="NullableIntegerArrayModel", fields=[ ( diff --git a/tests/postgres_tests/models.py b/tests/postgres_tests/models.py index e3118bc590..1563f6a35d 100644 --- a/tests/postgres_tests/models.py +++ b/tests/postgres_tests/models.py @@ -64,6 +64,10 @@ class DateTimeArrayModel(PostgreSQLModel): times = ArrayField(models.TimeField()) +class WithSizeArrayModel(PostgreSQLModel): + field = ArrayField(models.FloatField(), size=3) + + class NestedIntegerArrayModel(PostgreSQLModel): field = ArrayField(ArrayField(models.IntegerField())) diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index d930a01a1d..9ad4ec16e9 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -28,6 +28,7 @@ from .models import ( OtherTypesArrayModel, PostgreSQLModel, Tag, + WithSizeArrayModel, ) try: @@ -216,6 +217,16 @@ class TestQuerying(PostgreSQLTestCase): ] ) + def test_bulk_create_with_sized_arrayfield(self): + objs = WithSizeArrayModel.objects.bulk_create( + [ + WithSizeArrayModel(field=[1, 2]), + WithSizeArrayModel(field=[3, 4]), + ] + ) + self.assertEqual(objs[0].field, [1, 2]) + self.assertEqual(objs[1].field, [3, 4]) + def test_empty_list(self): NullableIntegerArrayModel.objects.create(field=[]) obj = ( |
