Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ protected function getDropForeignKeyInstructions(string $tableName, string $cons
{
$alter = sprintf(
'DROP FOREIGN KEY %s',
$constraint,
$this->quoteColumnName($constraint),
);

return new AlterInstructions([$alter]);
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,32 @@ public function testDropForeignKeyWithIdenticalMultipleColumns(): void
$this->assertFalse($this->adapter->hasForeignKey($table->getName(), [], 'ref_table_fk_2'));
}

public function testDropForeignKeyWithNameContainingWhitespace(): void
{
$refTable = new Table('ref_table', [], $this->adapter);
$refTable->addColumn('field1', 'string')->save();

$table = new Table('table', [], $this->adapter);
$key = (new ForeignKey())
->setName('ref table fk')
->setColumns(['ref_table_id'])
->setReferencedTable('ref_table')
->setReferencedColumns(['id']);
$table
->addColumn('ref_table_id', 'integer', ['signed' => false])
->addForeignKey($key)
->save();

$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 😢

// produce invalid SQL.
$this->adapter->dropForeignKey($table->getName(), [], 'ref table fk');

$this->assertFalse($this->adapter->hasForeignKey($table->getName(), [], 'ref table fk'));
}

public static function nonExistentForeignKeyColumnsProvider(): array
{
return [
Expand Down
Loading