mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
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.
This commit is contained in:
committed by
Sarah Boyce
parent
f5772de696
commit
22fc151bb8
@@ -167,6 +167,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=[
|
||||
|
||||
@@ -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()))
|
||||
|
||||
|
||||
@@ -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 = (
|
||||
|
||||
Reference in New Issue
Block a user