1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #34861 -- Fixed crash when adding GeneratedField with some expressions.

Co-authored-by: Simon Charette <charette.s@gmail.com>
This commit is contained in:
Paolo Melchiorre 2023-09-22 11:27:40 +02:00 committed by Mariusz Felisiak
parent 51d703a27f
commit 574ee4023e
2 changed files with 23 additions and 6 deletions

View File

@ -61,12 +61,10 @@ class GeneratedField(Field):
self.register_lookup(lookup, lookup_name=lookup_name) self.register_lookup(lookup, lookup_name=lookup_name)
def generated_sql(self, connection): def generated_sql(self, connection):
return self._resolved_expression.as_sql(
compiler = connection.ops.compiler("SQLCompiler")( compiler = connection.ops.compiler("SQLCompiler")(
self._query, connection=connection, using=None self._query, connection=connection, using=None
),
connection=connection,
) )
return compiler.compile(self._resolved_expression)
def check(self, **kwargs): def check(self, **kwargs):
databases = kwargs.get("databases") or [] databases = kwargs.get("databases") or []

View File

@ -32,6 +32,7 @@ from django.db.models import (
FloatField, FloatField,
ForeignKey, ForeignKey,
ForeignObject, ForeignObject,
GeneratedField,
Index, Index,
IntegerField, IntegerField,
JSONField, JSONField,
@ -50,7 +51,7 @@ from django.db.models import (
UUIDField, UUIDField,
Value, Value,
) )
from django.db.models.fields.json import KeyTextTransform 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, Upper
from django.db.models.indexes import IndexExpression from django.db.models.indexes import IndexExpression
from django.db.transaction import TransactionManagementError, atomic from django.db.transaction import TransactionManagementError, atomic
@ -810,6 +811,24 @@ class SchemaTests(TransactionTestCase):
# Introspection treats BLOBs as TextFields # Introspection treats BLOBs as TextFields
self.assertEqual(columns["bits"][0], "TextField") self.assertEqual(columns["bits"][0], "TextField")
@isolate_apps("schema")
@skipUnlessDBFeature("supports_json_field", "supports_stored_generated_columns")
def test_add_generated_field_with_kt_model(self):
class GeneratedFieldKTModel(Model):
data = JSONField()
status = GeneratedField(expression=KT("data__status"), db_persist=True)
class Meta:
app_label = "schema"
with CaptureQueriesContext(connection) as ctx:
with connection.schema_editor() as editor:
editor.create_model(GeneratedFieldKTModel)
self.assertIs(
any("None" in query["sql"] for query in ctx.captured_queries),
False,
)
@isolate_apps("schema") @isolate_apps("schema")
def test_add_auto_field(self): def test_add_auto_field(self):
class AddAutoFieldModel(Model): class AddAutoFieldModel(Model):