1
0
mirror of https://github.com/django/django.git synced 2025-01-03 06:55:47 +00:00

Fixed #34984 -- Skipped GeneratedFields when remaking tables on SQLite.

Regression in f333e3513e8bdf5ffeb6eeb63021c230082e6f95.t

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Co-authored-by: David Sanders <shang.xiao.sanders@gmail.com>
This commit is contained in:
Sarah Boyce 2023-11-22 10:01:23 +01:00 committed by Mariusz Felisiak
parent 0d407bcce5
commit 828082dad9
2 changed files with 38 additions and 1 deletions

View File

@ -107,6 +107,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
mapping = {
f.column: self.quote_name(f.column)
for f in model._meta.local_concrete_fields
if f.generated is False
}
# This maps field names (not columns) for things like unique_together
rename_mapping = {}
@ -163,7 +164,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# Remove any deleted fields
if delete_field:
del body[delete_field.name]
del mapping[delete_field.column]
mapping.pop(delete_field.column, None)
# Remove any implicit M2M tables
if (
delete_field.many_to_many

View File

@ -5801,6 +5801,42 @@ class OperationTests(OperationTestBase):
def test_remove_generated_field_virtual(self):
self._test_remove_generated_field(db_persist=False)
@skipUnlessDBFeature("supports_stored_generated_columns")
def test_add_field_after_generated_field(self):
app_label = "test_adfagf"
project_state = self.set_up_test_model(app_label)
operation_1 = migrations.AddField(
"Pony",
"generated",
models.GeneratedField(
expression=Value(1),
output_field=models.IntegerField(),
db_persist=True,
),
)
operation_2 = migrations.AddField(
"Pony",
"static",
models.IntegerField(default=2),
)
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)
project_state, new_state = new_state, new_state.clone()
pony_old = new_state.apps.get_model(app_label, "Pony").objects.create(weight=20)
self.assertEqual(pony_old.generated, 1)
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)
Pony = new_state.apps.get_model(app_label, "Pony")
pony_old = Pony.objects.get(pk=pony_old.pk)
self.assertEqual(pony_old.generated, 1)
self.assertEqual(pony_old.static, 2)
pony_new = Pony.objects.create(weight=20)
self.assertEqual(pony_new.generated, 1)
self.assertEqual(pony_new.static, 2)
class SwappableOperationTests(OperationTestBase):
"""