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:
- Addition in
function diffForeignKey():
if (strtolower($key1->getName()) !== strtolower($key2->getName())) {
return true;
}
- Changes in
function compareTables() lines 293-299
foreach ($oldForeignKeys as $oldKey => $oldForeignKey) {
$droppedForeignKeys[$oldKey] = $oldForeignKey;
}
foreach ($newForeignKeys as $newKey => $newForeignKey) {
$addedForeignKeys[$newKey] = $newForeignKey;
}
Bug Report
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
table_aand tabletable_b.table_ahave a FK namedFK_atobtotable_b.Method
up()will have:it should be:
Method
down()will have:it should be:
Possible fix:
Changes on my machine in file src/Schema/Comparator.php, that fixed the issue:
function diffForeignKey():function compareTables()lines293-299