Skip to content

Commit 30698f8

Browse files
authored
Quote constraint name in MySQL DROP FOREIGN KEY statement (#1088)
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.
1 parent 78a1e6b commit 30698f8

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/Db/Adapter/MysqlAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ protected function getDropForeignKeyInstructions(string $tableName, string $cons
10611061
{
10621062
$alter = sprintf(
10631063
'DROP FOREIGN KEY %s',
1064-
$constraint,
1064+
$this->quoteColumnName($constraint),
10651065
);
10661066

10671067
return new AlterInstructions([$alter]);

tests/TestCase/Db/Adapter/MysqlAdapterTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,32 @@ public function testDropForeignKeyWithIdenticalMultipleColumns(): void
18481848
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), [], 'ref_table_fk_2'));
18491849
}
18501850

1851+
public function testDropForeignKeyWithNameContainingWhitespace(): void
1852+
{
1853+
$refTable = new Table('ref_table', [], $this->adapter);
1854+
$refTable->addColumn('field1', 'string')->save();
1855+
1856+
$table = new Table('table', [], $this->adapter);
1857+
$key = (new ForeignKey())
1858+
->setName('ref table fk')
1859+
->setColumns(['ref_table_id'])
1860+
->setReferencedTable('ref_table')
1861+
->setReferencedColumns(['id']);
1862+
$table
1863+
->addColumn('ref_table_id', 'integer', ['signed' => false])
1864+
->addForeignKey($key)
1865+
->save();
1866+
1867+
$this->assertTrue($this->adapter->hasForeignKey($table->getName(), [], 'ref table fk'));
1868+
1869+
// The constraint name must be quoted in the DROP statement, otherwise
1870+
// names with whitespace (or numeric names auto-assigned by MariaDB 12)
1871+
// produce invalid SQL.
1872+
$this->adapter->dropForeignKey($table->getName(), [], 'ref table fk');
1873+
1874+
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), [], 'ref table fk'));
1875+
}
1876+
18511877
public static function nonExistentForeignKeyColumnsProvider(): array
18521878
{
18531879
return [

0 commit comments

Comments
 (0)