Skip to content

Bug with FK name changes #7356

@AlbertZawadzki

Description

@AlbertZawadzki

Bug Report

Q A
Version 4.3.3
Database MariaDB 10.2.44

Summary

Foreign key name changes are not properly recognized. The src/Schema/Comparator.php recognizes which FK's should be dropped and created, but both on drop and creation they have old names. While on drop it should have old name and on creation it should have a new one.

How to reproduce

  • Using SQL create 2 tables. Table table_a and table table_b.
CREATE TABLE table_a (id INT AUTO_INCREMENT NOT NULL, b_id INT DEFAULT NULL, PRIMARY KEY (id));
CREATE TABLE table_b (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY (id));
  • Let table_a have a FK named FK_atob to table_b.
ALTER TABLE table_a ADD CONSTRAINT FK_atob FOREIGN KEY (b_id) REFERENCES table_b (id)
  • Now create doctrine entities and create a migration

Method up() will have:

$this->addSql('ALTER TABLE `table_a` DROP FOREIGN KEY `FK_atob`');
$this->addSql('ALTER TABLE `table_a` ADD CONSTRAINT `FK_atob` FOREIGN KEY (b_id) REFERENCES `table_b` (id)');

it should be:

$this->addSql('ALTER TABLE `table_a` DROP FOREIGN KEY `FK_atob`');
$this->addSql('ALTER TABLE `table_a` ADD CONSTRAINT `FK_{hash_or_new_name}` FOREIGN KEY (b_id) REFERENCES `table_b`  (id)');

Method down() will have:

$this->addSql('ALTER TABLE `table_a` DROP FOREIGN KEY `FK_{hash_or_new_name}`');
$this->addSql('ALTER TABLE `table_a` ADD CONSTRAINT `FK_{hash_or_new_name}` FOREIGN KEY (b_id) REFERENCES `table_b`  (id)');

it should be:

$this->addSql('ALTER TABLE `table_a` DROP FOREIGN KEY `FK_{hash_or_new_name}`');
$this->addSql('ALTER TABLE `table_a` ADD CONSTRAINT `FK_atob` FOREIGN KEY (b_id) REFERENCES `table_b`  (id)');

Possible fix:

Changes on my machine in file src/Schema/Comparator.php, that fixed the issue:

  1. Addition in function diffForeignKey():
if (strtolower($key1->getName()) !== strtolower($key2->getName())) {
   return true;
}
  1. Changes in function compareTables() lines 293-299
foreach ($oldForeignKeys as $oldKey => $oldForeignKey) {
   $droppedForeignKeys[$oldKey] = $oldForeignKey;
}

foreach ($newForeignKeys as $newKey => $newForeignKey) {
   $addedForeignKeys[$newKey] = $newForeignKey;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions