MySQL adapter fails to drop multi-column foreign keys
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 4.5k
- Forks
- 884
- PR merge metrics
- No merged PRs in 30d
Description
MysqlAdapter::getDropForeignKeyByColumnsInstructions() fetches the same constraint name many times if a foreign key contains more than one column. It then creates the same DROP statement as many times as there are columns in the key. Furthermore, if the same column is used in many foreign keys, they all may be dropped (depending on ordering).
I didn't test this, but I believe the query could be fixed about so:
$columnNames = "'" . implode("','", $columns) . "'";
$columnCount = count($columns);
$sql = "select constraint_name
from information_schema.table_constraints
join information_schema.key_column_usage
using(constraint_name, table_schema, table_name)
where table_name = '$tableName'
and constraint_type = 'FOREIGN KEY'
and column_name in ($columnNames)
group by constraint_name
having count(column_name) = $columnCount";
This would fix my use case, but I don't know if there are other cases where dropping multiple constraints would be required.
EDIT: tested the query and fixed the constraint_type check.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at MysqlAdapter::getDropForeignKeyByColumnsInstructions() and inspect how it finds constraints and builds DROP statements. Reproduce the multi-column and shared-column cases described in the issue, then verify that each matching constraint is identified once and only the intended constraints are dropped.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mysql, php
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100