diff options
| author | Simon Charette <charette.s@gmail.com> | 2025-12-23 01:25:56 -0500 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2025-12-31 10:47:20 -0500 |
| commit | 2ca2afdffdc7a33344f3189965b6ebc92196dc10 (patch) | |
| tree | a4a23e46f93c2ffa75027203dadd95bb6caa4eae | |
| parent | 8346657aaff7da999dca32574e6595f8c9543a0c (diff) | |
[5.2.x] Refs #33647 -- Fixed silent data truncation in bulk_create on Postgres.
Regression in a16eedcf9c69d8a11d94cac1811018c5b996d491.
The UNNEST strategy is affected by the same problem bulk_update has wrt/
to silent data truncation due to its usage of db_type which always returns
a parametrized subtype.
Backport of d6ae2ed868e43671afc4d433c3d8f4d27f7eb555 from main.
| -rw-r--r-- | django/db/backends/postgresql/compiler.py | 4 | ||||
| -rw-r--r-- | docs/releases/5.2.10.txt | 6 | ||||
| -rw-r--r-- | tests/backends/postgresql/test_compilation.py | 10 |
3 files changed, 18 insertions, 2 deletions
diff --git a/django/db/backends/postgresql/compiler.py b/django/db/backends/postgresql/compiler.py index 38b61c4898..fcd7faaf35 100644 --- a/django/db/backends/postgresql/compiler.py +++ b/django/db/backends/postgresql/compiler.py @@ -62,7 +62,9 @@ class SQLInsertCompiler(BaseSQLInsertCompiler): or any(any(hasattr(value, "as_sql") for value in row) for row in value_rows) ): return super().assemble_as_sql(fields, value_rows) - db_types = [field.db_type(self.connection) for field in fields] + # Manually remove parameters from `db_type` to ensure no data + # truncation takes place (e.g. varchar[] instead of varchar(50)[]). + db_types = [field.db_type(self.connection).split("(")[0] for field in fields] return InsertUnnest(["(%%s)::%s[]" % db_type for db_type in db_types]), [ list(map(list, zip(*value_rows))) ] diff --git a/docs/releases/5.2.10.txt b/docs/releases/5.2.10.txt index 0b7ab06d54..6be8f79885 100644 --- a/docs/releases/5.2.10.txt +++ b/docs/releases/5.2.10.txt @@ -4,11 +4,15 @@ Django 5.2.10 release notes *Expected January 6, 2026* -Django 5.2.10 fixes several bugs in 5.2.9. +Django 5.2.10 fixes a data loss bug in Django 5.2 and one bug related to +support for Python 3.14. Bugfixes ======== +* Fixed a bug in Django 5.2 where data exceeding ``max_length`` was silently + truncated by :meth:`.QuerySet.bulk_create` on PostgreSQL (:ticket:`33647`). + * Fixed a bug where management command colorized help (introduced in Python 3.14) ignored the :option:`--no-color` option and the :envvar:`DJANGO_COLORS` setting (:ticket:`36376`). diff --git a/tests/backends/postgresql/test_compilation.py b/tests/backends/postgresql/test_compilation.py index 11c4015e01..2053c18a1a 100644 --- a/tests/backends/postgresql/test_compilation.py +++ b/tests/backends/postgresql/test_compilation.py @@ -3,6 +3,7 @@ from datetime import date from django.db import connection from django.db.models.expressions import RawSQL +from django.db.utils import DataError from django.test import TestCase from ..models import Article, Reporter, Square @@ -42,3 +43,12 @@ class BulkCreateUnnestTests(TestCase): self.assertEqual( [article.reporter for article in articles], [reporter, reporter] ) + + def test_parametrized_db_type(self): + with self.assertRaises(DataError): + Reporter.objects.bulk_create( + [ + Reporter(), + Reporter(first_name="a" * 31), + ] + ) |
