Skip to content

Commit

Permalink
Fixed #34877 -- Fixed migrations crash when adding GeneratedField wit…
Browse files Browse the repository at this point in the history
…h output_field with params.
  • Loading branch information
pauloxnet authored and felixxm committed Sep 28, 2023
1 parent 5e4b75b commit e7e8eb4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions django/db/models/fields/generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,6 @@ def get_internal_type(self):

def db_parameters(self, connection):
return self.output_field.db_parameters(connection)

def db_type_parameters(self, connection):
return self.output_field.db_type_parameters(connection)
7 changes: 7 additions & 0 deletions tests/model_fields/test_generatedfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ def test_output_field(self):
field._resolved_expression.output_field.db_type(connection),
)

@skipUnlessDBFeature("supports_collation_on_charfield")
def test_db_type_parameters(self):
db_type_parameters = self.output_field_model._meta.get_field(
"lower_name"
).db_type_parameters(connection)
self.assertEqual(db_type_parameters["max_length"], 11)

def test_model_with_params(self):
m = self.params_model.objects.create()
m = self._refresh_if_needed(m)
Expand Down
20 changes: 19 additions & 1 deletion tests/schema/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import itertools
import unittest
from copy import copy
from decimal import Decimal
from unittest import mock

from django.core.exceptions import FieldError
Expand Down Expand Up @@ -52,7 +53,7 @@
Value,
)
from django.db.models.fields.json import KT, KeyTextTransform
from django.db.models.functions import Abs, Cast, Collate, Lower, Random, Upper
from django.db.models.functions import Abs, Cast, Collate, Lower, Random, Round, Upper
from django.db.models.indexes import IndexExpression
from django.db.transaction import TransactionManagementError, atomic
from django.test import TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature
Expand Down Expand Up @@ -829,6 +830,23 @@ class Meta:
False,
)

@isolate_apps("schema")
@skipUnlessDBFeature("supports_stored_generated_columns")
def test_add_generated_field_with_output_field(self):
class GeneratedFieldOutputFieldModel(Model):
price = DecimalField(max_digits=7, decimal_places=2)
vat_price = GeneratedField(
expression=Round(F("price") * Value(Decimal("1.22")), 2),
db_persist=True,
output_field=DecimalField(max_digits=8, decimal_places=2),
)

class Meta:
app_label = "schema"

with connection.schema_editor() as editor:
editor.create_model(GeneratedFieldOutputFieldModel)

@isolate_apps("schema")
def test_add_auto_field(self):
class AddAutoFieldModel(Model):
Expand Down

0 comments on commit e7e8eb4

Please sign in to comment.