Quote constraint name in MySQL DROP FOREIGN KEY statement#1088
Merged
Conversation
The MySQL adapter built `DROP FOREIGN KEY <name>` without quoting the
constraint identifier. That produces invalid SQL whenever the constraint
name is not a bare identifier, e.g. names containing whitespace, or the
numeric names ("1", "2", ...) that MariaDB 12 auto-assigns to foreign
keys created without an explicit name.
Quote the identifier via quoteColumnName(), matching the Postgres and
SQL Server adapters, which already quote the dropped constraint name.
markstory
reviewed
May 22, 2026
| $this->assertTrue($this->adapter->hasForeignKey($table->getName(), [], 'ref table fk')); | ||
|
|
||
| // The constraint name must be quoted in the DROP statement, otherwise | ||
| // names with whitespace (or numeric names auto-assigned by MariaDB 12) |
Member
There was a problem hiding this comment.
MariaDB off doing weird stuff again 😢
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The MySQL adapter built
DROP FOREIGN KEY <name>without quoting the constraint identifier. Whenever the name is not a bare identifier, the resulting SQL is invalid. Two cases hit this:DROP FOREIGN KEY my fkis a syntax error (dropForeignKey does not allow removal of constraints containing whitespaces phinx#924).1,2, …) to foreign keys created without an explicit name, so a later drop emitsDROP FOREIGN KEY 1, which fails.The fix quotes the identifier via
quoteColumnName(), bringing the MySQL adapter in line with the Postgres and SQL Server adapters, which already quote the dropped constraint name.Relation to phinx
This ports the relevant part of cakephp/phinx#2413 to the builtin backend. The other parts of that phinx PR — guarding against PHP int-casting numeric array keys during foreign-key introspection — do not apply to the builtin backend:
MysqlSchemaDialect::describeForeignKeys(), which returnsarray_values(...)with the name preserved as a string in each entry.getDropForeignKeyByColumnsInstructions()already iterates by value and reads$key['name'], so it never relies on the (int-castable) array key.Only the missing identifier quoting in
getDropForeignKeyInstructions()remained, which this PR addresses.Includes a regression test that creates a foreign key whose name contains whitespace and drops it by name; on the previous code it fails with a SQL syntax error.