diff --git a/src/Phinx/Db/Adapter/MysqlAdapter.php b/src/Phinx/Db/Adapter/MysqlAdapter.php index 9753691f2..bc758f774 100644 --- a/src/Phinx/Db/Adapter/MysqlAdapter.php +++ b/src/Phinx/Db/Adapter/MysqlAdapter.php @@ -971,6 +971,7 @@ protected function getForeignKeys(string $tableName): array $tableName, )); foreach ($rows as $row) { + $foreignKeys[$row['CONSTRAINT_NAME']]['constraint'] = $row['CONSTRAINT_NAME']; $foreignKeys[$row['CONSTRAINT_NAME']]['table'] = $row['TABLE_NAME']; $foreignKeys[$row['CONSTRAINT_NAME']]['columns'][] = $row['COLUMN_NAME']; $foreignKeys[$row['CONSTRAINT_NAME']]['referenced_table'] = $row['REFERENCED_TABLE_NAME']; @@ -999,7 +1000,7 @@ protected function getAddForeignKeyInstructions(Table $table, ForeignKey $foreig protected function getDropForeignKeyInstructions(string $tableName, string $constraint): AlterInstructions { $alter = sprintf( - 'DROP FOREIGN KEY %s', + 'DROP FOREIGN KEY `%s`', $constraint, ); @@ -1019,9 +1020,9 @@ protected function getDropForeignKeyByColumnsInstructions(string $tableName, arr $matches = []; $foreignKeys = $this->getForeignKeys($tableName); - foreach ($foreignKeys as $name => $key) { - if (array_map('mb_strtolower', $key['columns']) === $columns) { - $matches[] = $name; + foreach ($foreignKeys as $foreignKey) { + if (array_map('mb_strtolower', $foreignKey['columns']) === $columns) { + $matches[] = $foreignKey['constraint']; } }