From 73869a51632b9f826d4d7b9e888f81d60b16e8b6 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 7 Nov 2023 21:47:37 -0500 Subject: [5.0.x] Refs #30446, Refs #34944 -- Fixed crash when adding GeneratedField with string Value(). This should allow smarter output_field inferring in functions dealing with text expressions. Regression in f333e3513e8bdf5ffeb6eeb63021c230082e6f95. Backport of 8b1acc0440418ac8f45ba48e2dfcf5126c83341b from main --- tests/expressions/tests.py | 1 + tests/schema/tests.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) (limited to 'tests') diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 073e2e9258..9743d024b1 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -2227,6 +2227,7 @@ class ValueTests(TestCase): with self.subTest(type=type(value)): expr = Value(value) self.assertIsInstance(expr.output_field, output_field_type) + self.assertEqual(Value("foo").output_field.max_length, 3) def test_resolve_output_field_failure(self): msg = "Cannot resolve expression type, unknown output_field" diff --git a/tests/schema/tests.py b/tests/schema/tests.py index e1e3c7d3c6..5488013bfc 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -848,6 +848,23 @@ class SchemaTests(TransactionTestCase): False, ) + @isolate_apps("schema") + @skipUnlessDBFeature("supports_stored_generated_columns") + def test_add_generated_field_with_string_value(self): + class GeneratedFieldStringValueModel(Model): + value = GeneratedField(expression=Value("static string"), db_persist=True) + + class Meta: + app_label = "schema" + + with CaptureQueriesContext(connection) as ctx: + with connection.schema_editor() as editor: + editor.create_model(GeneratedFieldStringValueModel) + self.assertIs( + any("None" in query["sql"] for query in ctx.captured_queries), + False, + ) + @isolate_apps("schema") @skipUnlessDBFeature("supports_stored_generated_columns") def test_add_generated_field_with_output_field(self): -- cgit v1.3