Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket #34417 - Remove unnecessary recreation of foreign key constrains while dropping index. #18116

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions django/db/backends/base/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class BaseDatabaseFeatures:
# Does the database have a copy of the zoneinfo database?
has_zoneinfo_database = True

# Does the backend require the foreign key constraints to be recreated
# when dropping an index?
requires_fk_constraints_to_be_recreated = False

# When performing a GROUP BY, is an ORDER BY NULL required
# to remove any ordering?
requires_explicit_null_ordering_when_grouping = False
Expand Down
5 changes: 5 additions & 0 deletions django/db/backends/base/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,11 @@ def _alter_field(
fks_dropped = set()
if (
self.connection.features.supports_foreign_keys
and (
self.connection.features.requires_fk_constraints_to_be_recreated
or not old_field.related_model == new_field.related_model
or getattr(new_field, "db_constraint", True) is False
)
and old_field.remote_field
and old_field.db_constraint
and self._field_should_be_altered(
Expand Down
1 change: 1 addition & 0 deletions django/db/backends/mysql/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_regex_backreferencing = False
supports_date_lookup_using_string = False
supports_timezones = False
requires_fk_constraints_to_be_recreated = True
requires_explicit_null_ordering_when_grouping = True
atomic_transactions = False
can_clone_databases = True
Expand Down
36 changes: 36 additions & 0 deletions tests/schema/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,42 @@ class Meta:
editor.alter_field(LocalBook, old_field, new_field, strict=True)
self.assertForeignKeyExists(LocalBook, "author_id", "schema_author")

@skipUnlessDBFeature("supports_foreign_keys")
def test_fk_constraint_index_drop(self):
with connection.schema_editor() as editor:
editor.create_model(Author)
editor.create_model(Book)

author = Author.objects.create(name="Alice")
Book.objects.create(
author=author,
title="Much Ado About Foreign Keys",
pub_date=datetime.datetime.now(),
)

old_field = Book._meta.get_field("author")
new_field = ForeignKey(Author, CASCADE, db_index=False)
new_field.set_attributes_from_name("author")
with (
CaptureQueriesContext(connection) as ctx,
connection.schema_editor() as editor,
):
editor.alter_field(Book, old_field, new_field, strict=True)

if connection.features.requires_fk_constraints_to_be_recreated:
self.assertTrue(
any(
"DROP FOREIGN KEY" in query["sql"] for query in ctx.captured_queries
)
)
self.assertTrue(
any("ADD CONSTRAINT" in query["sql"] for query in ctx.captured_queries)
)
else:
self.assertFalse(
any("DROP CONSTRAINT" in query["sql"] for query in ctx.captured_queries)
)

@skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
def test_alter_o2o_to_fk(self):
"""
Expand Down