From 2ca2afdffdc7a33344f3189965b6ebc92196dc10 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 23 Dec 2025 01:25:56 -0500 Subject: [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. --- django/db/backends/postgresql/compiler.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'django') 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))) ] -- cgit v1.3