mirror of
https://github.com/django/django.git
synced 2025-01-15 21:02:52 +00:00
[5.0.x] Refs #34936 -- Added test for altering DecimalField with db_default to non-nullable.
Backport of 1944f490f91e44e1378d53420b64a78163aea815 from main
This commit is contained in:
parent
f1fbd061ac
commit
06b05c748f
@ -1,4 +1,5 @@
|
|||||||
import math
|
import math
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
from django.core.exceptions import FieldDoesNotExist
|
from django.core.exceptions import FieldDoesNotExist
|
||||||
from django.db import IntegrityError, connection, migrations, models, transaction
|
from django.db import IntegrityError, connection, migrations, models, transaction
|
||||||
@ -2219,6 +2220,43 @@ class OperationTests(OperationTestBase):
|
|||||||
pony = project_state.apps.get_model(app_label, "pony").objects.create(weight=1)
|
pony = project_state.apps.get_model(app_label, "pony").objects.create(weight=1)
|
||||||
self.assertIsNone(pony.green)
|
self.assertIsNone(pony.green)
|
||||||
|
|
||||||
|
def test_alter_field_change_nullable_to_decimal_database_default_not_null(self):
|
||||||
|
app_label = "test_alflcntdddn"
|
||||||
|
project_state = self.set_up_test_model(app_label)
|
||||||
|
operation_1 = migrations.AddField(
|
||||||
|
"Pony",
|
||||||
|
"height",
|
||||||
|
models.DecimalField(null=True, max_digits=5, decimal_places=2),
|
||||||
|
)
|
||||||
|
operation_2 = migrations.AlterField(
|
||||||
|
"Pony",
|
||||||
|
"height",
|
||||||
|
models.DecimalField(
|
||||||
|
max_digits=5, decimal_places=2, db_default=Decimal("12.22")
|
||||||
|
),
|
||||||
|
)
|
||||||
|
table_name = f"{app_label}_pony"
|
||||||
|
self.assertColumnNotExists(table_name, "height")
|
||||||
|
# Add field.
|
||||||
|
new_state = project_state.clone()
|
||||||
|
operation_1.state_forwards(app_label, new_state)
|
||||||
|
with connection.schema_editor() as editor:
|
||||||
|
operation_1.database_forwards(app_label, editor, project_state, new_state)
|
||||||
|
self.assertColumnExists(table_name, "height")
|
||||||
|
old_pony = new_state.apps.get_model(app_label, "pony").objects.create(weight=1)
|
||||||
|
self.assertIsNone(old_pony.height)
|
||||||
|
# Alter field.
|
||||||
|
project_state, new_state = new_state, new_state.clone()
|
||||||
|
operation_2.state_forwards(app_label, new_state)
|
||||||
|
with connection.schema_editor() as editor:
|
||||||
|
operation_2.database_forwards(app_label, editor, project_state, new_state)
|
||||||
|
old_pony.refresh_from_db()
|
||||||
|
self.assertEqual(old_pony.height, Decimal("12.22"))
|
||||||
|
pony = new_state.apps.get_model(app_label, "pony").objects.create(weight=2)
|
||||||
|
if not connection.features.can_return_columns_from_insert:
|
||||||
|
pony.refresh_from_db()
|
||||||
|
self.assertEqual(pony.height, Decimal("12.22"))
|
||||||
|
|
||||||
@skipIfDBFeature("interprets_empty_strings_as_nulls")
|
@skipIfDBFeature("interprets_empty_strings_as_nulls")
|
||||||
def test_alter_field_change_blank_nullable_database_default_to_not_null(self):
|
def test_alter_field_change_blank_nullable_database_default_to_not_null(self):
|
||||||
app_label = "test_alflcbnddnn"
|
app_label = "test_alflcbnddnn"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user