Skip to content

Quote constraint name in MySQL DROP FOREIGN KEY statement#1088

Merged
markstory merged 1 commit into
5.xfrom
fix-mysql-drop-foreign-key-quoting
May 22, 2026
Merged

Quote constraint name in MySQL DROP FOREIGN KEY statement#1088
markstory merged 1 commit into
5.xfrom
fix-mysql-drop-foreign-key-quoting

Conversation

@dereuromark
Copy link
Copy Markdown
Member

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:

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:

  • Foreign-key reflection is delegated to CakePHP core's MysqlSchemaDialect::describeForeignKeys(), which returns array_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.

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.
$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)
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.

MariaDB off doing weird stuff again 😢

@markstory markstory merged commit 30698f8 into 5.x May 22, 2026
13 of 14 checks passed
@markstory markstory deleted the fix-mysql-drop-foreign-key-quoting branch May 22, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants