mirror of
https://github.com/django/django.git
synced 2025-06-05 03:29:12 +00:00
[5.0.x] Refs #35149 -- Made equivalent db_default alterations noops.
This allows for an easier transition of preserving the literal nature of non-compilable db_default. Backport of fe1cb62f5c3f87fafc4a6b52fee2ccc6c80c41e2 from main
This commit is contained in:
parent
3e7a30fb3a
commit
914eee1a9b
@ -1646,6 +1646,14 @@ class BaseDatabaseSchemaEditor:
|
|||||||
):
|
):
|
||||||
old_kwargs.pop("to", None)
|
old_kwargs.pop("to", None)
|
||||||
new_kwargs.pop("to", None)
|
new_kwargs.pop("to", None)
|
||||||
|
# db_default can take many form but result in the same SQL.
|
||||||
|
if (
|
||||||
|
old_kwargs.get("db_default")
|
||||||
|
and new_kwargs.get("db_default")
|
||||||
|
and self.db_default_sql(old_field) == self.db_default_sql(new_field)
|
||||||
|
):
|
||||||
|
old_kwargs.pop("db_default")
|
||||||
|
new_kwargs.pop("db_default")
|
||||||
return self.quote_name(old_field.column) != self.quote_name(
|
return self.quote_name(old_field.column) != self.quote_name(
|
||||||
new_field.column
|
new_field.column
|
||||||
) or (old_path, old_args, old_kwargs) != (new_path, new_args, new_kwargs)
|
) or (old_path, old_args, old_kwargs) != (new_path, new_args, new_kwargs)
|
||||||
|
@ -2319,6 +2319,23 @@ class SchemaTests(TransactionTestCase):
|
|||||||
columns = self.column_classes(Author)
|
columns = self.column_classes(Author)
|
||||||
self.assertIn("(missing)", columns["description"][1].default)
|
self.assertIn("(missing)", columns["description"][1].default)
|
||||||
|
|
||||||
|
@isolate_apps("schema")
|
||||||
|
def test_db_default_equivalent_sql_noop(self):
|
||||||
|
class Author(Model):
|
||||||
|
name = TextField(db_default=Value("foo"))
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
app_label = "schema"
|
||||||
|
|
||||||
|
with connection.schema_editor() as editor:
|
||||||
|
editor.create_model(Author)
|
||||||
|
|
||||||
|
new_field = TextField(db_default="foo")
|
||||||
|
new_field.set_attributes_from_name("name")
|
||||||
|
new_field.model = Author
|
||||||
|
with connection.schema_editor() as editor, self.assertNumQueries(0):
|
||||||
|
editor.alter_field(Author, Author._meta.get_field("name"), new_field)
|
||||||
|
|
||||||
@skipUnlessDBFeature(
|
@skipUnlessDBFeature(
|
||||||
"supports_column_check_constraints", "can_introspect_check_constraints"
|
"supports_column_check_constraints", "can_introspect_check_constraints"
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user