Skip to content

Ignore constraint operations on tables being dropped in SQLite migrations (#39761) - #39768

Open
SkyDevLab wants to merge 2 commits into
dotnet:mainfrom
SkyDevLab:fix/39761-sqlite-drop-fk-cycle
Open

SkyDevLab wants to merge 2 commits into
dotnet:mainfrom
SkyDevLab:fix/39761-sqlite-drop-fk-cycle

Conversation

@SkyDevLab

Copy link
Copy Markdown
Contributor

Fixes #39761

Description

When diffing two models where tables in a foreign-key cycle are removed, the model differ outputs a DropForeignKeyOperation followed by DropTableOperation instances to break the table-drop dependency cycle.

In SQLite, SqliteMigrationsSqlGenerator groups table rebuild operations. When a table does not exist in the target model (e.g. because it is being dropped), table == null and the generator skips rebuilding the table. Previously, the generator did not remove rebuildContext.OperationsToReplace when skipping the rebuild for a dropped table. Consequently, DropForeignKeyOperation was left in the operations list, and SqliteMigrationsSqlGenerator.Generate(DropForeignKeyOperation) threw NotSupportedException.

This PR checks if a skipped rebuild corresponds to a table that is being dropped (via operations.OfType<DropTableOperation>()). If so, it removes all operations targeting that table from operations, because dropping the table via DROP TABLE already removes all constraints and columns in SQLite.

Tests

  • Added DropForeignKey_ignored_when_table_is_dropped to SqliteMigrationsSqlGeneratorTest.
  • All 85 tests in SqliteMigrationsSqlGeneratorTest pass.

@SkyDevLab
SkyDevLab requested a review from a team as a code owner November 23, 2026 17:23
{
foreach (var operationToReplace in rebuildContext.OperationsToReplace)
{
operations.Remove(operationToReplace);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes SQL generation, but the drops still fail for valid, populated cyclic tables when SQLite FK enforcement is enabled (DROP TABLE "Revisions" fails with FOREIGN KEY constraint failed). Please disable and restore FK enforcement with transaction-suppressed PRAGMA commands around the drops, and change the test to a functional one that seeds the cycle, applies the migration, and verifies both tables are removed.

@AndriySvyryd AndriySvyryd self-assigned this Nov 24, 2026
@SkyDevLab

Copy link
Copy Markdown
Contributor Author

@AndriySvyryd Yes, definitely! I've updated the PR with the requested changes:

  • SqliteMigrationsSqlGenerator now wraps table drops with transaction-suppressed PRAGMA foreign_keys = 0; and PRAGMA foreign_keys = 1; so SQLite does not reject dropping tables with active foreign key references/cycles when FK enforcement is enabled.
  • Added a functional test Drop_table_with_foreign_key_cycle in MigrationsSqliteTest that defines the cyclic model, seeds rows creating the cycle, applies the migration that drops both tables, and asserts both tables are removed.
  • Updated the unit test assertions in SqliteMigrationsSqlGeneratorTest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQLite SQL generation fails when dropping both tables in a foreign-key cycle

2 participants