Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #33647 -- bulk_update silently truncating values for size limied fields(PostgreSQL) #17366

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion django/db/models/functions/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ class Cast(Func):
function = "CAST"
template = "%(function)s(%(expressions)s AS %(db_type)s)"

def __init__(self, expression, output_field):
def __init__(self, expression, output_field, charfield_without_max_length=False):
self.charfield_without_max_length = charfield_without_max_length
super().__init__(expression, output_field=output_field)

def as_sql(self, compiler, connection, **extra_context):
extra_context["db_type"] = self.output_field.cast_db_type(connection)
if (
self.charfield_without_max_length
and self.output_field.get_internal_type() == "CharField"
):
extra_context["db_type"] = connection.ops.cast_char_field_without_max_length
return super().as_sql(compiler, connection, **extra_context)

def as_sqlite(self, compiler, connection, **extra_context):
Expand Down
6 changes: 5 additions & 1 deletion django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,11 @@ def bulk_update(self, objs, fields, batch_size=None):
when_statements.append(When(pk=obj.pk, then=attr))
case_statement = Case(*when_statements, output_field=field)
if requires_casting:
case_statement = Cast(case_statement, output_field=field)
case_statement = Cast(
case_statement,
output_field=field,
charfield_without_max_length=True,
)
update_kwargs[field.attname] = case_statement
updates.append(([obj.pk for obj in batch_objs], update_kwargs))
rows_updated = 0
Expand Down
15 changes: 14 additions & 1 deletion tests/queries/test_bulk_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.core.exceptions import FieldDoesNotExist
from django.db.models import F
from django.db.models.functions import Lower
from django.db.utils import IntegrityError
from django.db.utils import DataError, IntegrityError
from django.test import TestCase, override_settings, skipUnlessDBFeature

from .models import (
Expand Down Expand Up @@ -261,6 +261,19 @@ def test_ipaddressfield(self):
CustomDbColumn.objects.filter(ip_address=ip), models
)

@skipUnlessDBFeature("supports_cast_with_precision")
def test_charfield_constraint(self):
article1 = Article.objects.create(
name="a" * 20, created=datetime.datetime.today()
)
article2 = Article.objects.create(
name="a" * 20, created=datetime.datetime.today()
)
article1.name = "b" * 50
article2.name = "b" * 50
with self.assertRaises(DataError):
Article.objects.bulk_update([article1, article2], ["name"])

def test_datetime_field(self):
articles = [
Article.objects.create(name=str(i), created=datetime.datetime.today())
Expand Down