Skip to content

Conversation

santysisi
Copy link

Q A
Type bug
Fixed issues #7124

Summary

This PR fixes an issue where unnamed foreign keys were not dropped in SQLite during schema diffs.

@santysisi
Copy link
Author

I added the test to SchemaManagerFunctionalTestCase instead of SQLiteSchemaManagerTest because I believe it's relevant for all platforms, not just SQLite.
However, if this test should be scoped to SQLite only, please let me know and I’ll move it accordingly.

SQLite ignored unnamed foreign key drops during schema diffs,
leading to duplicate constraints. This uses the diff key as a
fallback name to ensure unnamed FKs are removed properly.
@santysisi santysisi force-pushed the fix/sqlite-drop-unnamed-foreign-keys branch from 54bae1a to c2852f3 Compare September 5, 2025 01:11
@@ -929,8 +930,8 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
);
}

foreach ($diff->getDroppedForeignKeys() as $constraint) {
$constraintName = $constraint->getName();
foreach ($diff->getDroppedForeignKeys() as $name => $constraint) {
Copy link
Member

Choose a reason for hiding this comment

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

This is against the effort made in #7102. The constraint name is represented by the constraint object itself, not by some array key.

Copy link
Author

Choose a reason for hiding this comment

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

Got it, thanks for pointing that out! I’ll look into applying a different approach that aligns better with the changes from #7102 as soon as possible.

Copy link
Member

@morozov morozov Sep 10, 2025

Choose a reason for hiding this comment

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

It might be a good idea to just deprecate the scenario when the constraint being dropped doesn't have a name. Currently, we're not doing anything, which causes a bug. Making it work correctly would require a deeper redesign of schema management.

Just for context, the fundamental flaw is the following. The test attempts to perform a schema migration in the following steps:

  1. Define two versions of a table.
  2. Calculate a difference between them.
  3. Apply the difference.

The problem is that:

  1. The calculation of the difference is non-deterministic (you cannot reliably tell whether a given column was renamed or dropped and created). This doesn't apply to this specific case, but still.
  2. SQLite doesn't support ALTER TABLE. The DBAL has to recreate it from scratch and migrate the data.

The combination of these flaws leads to the fact that we cannot migrate schemas with unnamed constraints. A proper design for SQLite would not involve the diff at all. For SQLite, it would use the expected table schema directly.

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.

3 participants