diff options
| author | Simon Charette <charette.s@gmail.com> | 2024-01-28 12:02:33 -0500 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2024-02-04 09:39:41 +0100 |
| commit | e67d7d70fa10c06aca36b9057f82054eda45269d (patch) | |
| tree | f39f38b19211706bebf54dc8a8bba1066a895fcd /tests/schema | |
| parent | fe1cb62f5c3f87fafc4a6b52fee2ccc6c80c41e2 (diff) | |
Fixed #35149 -- Fixed crashes of db_default with unresolvable output field.
Field.db_default accepts either literal Python values or compilables
(as_sql) and wrap the former ones in Value internally.
While 1e38f11 added support for automatic resolving of output fields for
types such as str, int, float, and other unambigous ones it's cannot do
so for all types such as dict or even contrib.postgres and contrib.gis
primitives.
When a literal, non-compilable, value is provided it likely make the
most sense to bind its output field to the field its attached to avoid
forcing the user to provide an explicit `Value(output_field)`.
Thanks David Sanders for the report.
Diffstat (limited to 'tests/schema')
| -rw-r--r-- | tests/schema/tests.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 04ad299aa6..ced3367f00 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -7,6 +7,7 @@ from unittest import mock from django.core.exceptions import FieldError from django.core.management.color import no_style +from django.core.serializers.json import DjangoJSONEncoder from django.db import ( DatabaseError, DataError, @@ -2333,6 +2334,26 @@ class SchemaTests(TransactionTestCase): with connection.schema_editor() as editor, self.assertNumQueries(0): editor.alter_field(Author, Author._meta.get_field("name"), new_field) + @isolate_apps("schema") + def test_db_default_output_field_resolving(self): + class Author(Model): + data = JSONField( + encoder=DjangoJSONEncoder, + db_default={ + "epoch": datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc) + }, + ) + + class Meta: + app_label = "schema" + + with connection.schema_editor() as editor: + editor.create_model(Author) + + author = Author.objects.create() + author.refresh_from_db() + self.assertEqual(author.data, {"epoch": "1970-01-01T00:00:00Z"}) + @skipUnlessDBFeature( "supports_column_check_constraints", "can_introspect_check_constraints" ) |
