Skip to content

Commit 673492c

Browse files
authored
Merge pull request #7125 from santysisi/fix/duplicate-unnamed-fk-constraint-overwrite
Deprecate overwriting existing foreign key constraints
2 parents aa6b906 + 2e5dbaf commit 673492c

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

UPGRADE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ awareness about deprecated code.
88

99
# Upgrade to 4.4
1010

11+
## Deprecated overwriting foreign key constraints
12+
13+
Adding a foreign key constraint with a name that matches an existing one, whether explicitly specified or
14+
auto-generated, has been deprecated. In order to replace an existing constraint, drop it first
15+
via `dropForeignKey()`.
16+
1117
## Deprecated `View` features
1218

1319
The `View` constructor has been marked as internal. Use `View::editor()` to instantiate an editor and

src/Schema/Table.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,14 @@ protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint): s
938938
);
939939

940940
$name = $this->normalizeIdentifier($name);
941+
if (isset($this->_fkConstraints[$name])) {
942+
Deprecation::trigger(
943+
'doctrine/dbal',
944+
'https://github.com/doctrine/dbal/pull/7125',
945+
'Overwriting an existing foreign key constraint ("%s") is deprecated.',
946+
$name,
947+
);
948+
}
941949

942950
$this->_fkConstraints[$name] = $constraint;
943951

tests/Schema/TableTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,4 +1768,22 @@ public function testInvalidPrimaryKeyIndex(): void
17681768
$this->expectException(InvalidState::class);
17691769
$table->getPrimaryKeyConstraint();
17701770
}
1771+
1772+
public function testOverwritingForeignKeyConstraint(): void
1773+
{
1774+
$table = Table::editor()
1775+
->setUnquotedName('foo')
1776+
->setColumns(
1777+
Column::editor()
1778+
->setUnquotedName('id')
1779+
->setTypeName(Types::INTEGER)
1780+
->create(),
1781+
)
1782+
->create();
1783+
1784+
$table->addForeignKeyConstraint('bar', ['id'], ['id']);
1785+
1786+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/7125');
1787+
$table->addForeignKeyConstraint('baz', ['id'], ['id']);
1788+
}
17711789
}

0 commit comments

Comments
 (0)