cakephp / cakephp/phinx

MySQL adapter fails to drop multi-column foreign keys

Open
#2,056 2 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.