2727use Doctrine \DBAL \Types ;
2828
2929use function array_merge ;
30+ use function assert ;
3031use function count ;
3132use function explode ;
3233use function implode ;
@@ -862,6 +863,7 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
862863 $ oldTable = $ diff ->getOldTable ();
863864 $ foreignKeys = $ oldTable ->getForeignKeys ();
864865 $ nameMap = $ this ->getDiffColumnNameMap ($ diff );
866+ $ keysByName = [];
865867
866868 foreach ($ foreignKeys as $ key => $ constraint ) {
867869 $ changed = false ;
@@ -884,6 +886,17 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
884886 $ changed = true ;
885887 }
886888
889+ $ constraintName = $ constraint ->getObjectName ();
890+
891+ if ($ constraintName !== null ) {
892+ $ constraintKey = strtolower (
893+ $ constraintName ->getIdentifier ()
894+ ->getValue (),
895+ );
896+
897+ $ keysByName [$ constraintKey ] = $ key ;
898+ }
899+
887900 if (! $ changed ) {
888901 continue ;
889902 }
@@ -905,7 +918,8 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
905918 ->getValue (),
906919 );
907920
908- unset($ foreignKeys [$ constraintKey ]);
921+ assert (isset ($ keysByName [$ constraintKey ]));
922+ unset($ foreignKeys [$ keysByName [$ constraintKey ]], $ keysByName [$ constraintKey ]);
909923 }
910924
911925 foreach ($ diff ->getAddedForeignKeys () as $ constraint ) {
@@ -917,7 +931,10 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
917931 ->getValue (),
918932 );
919933
920- $ foreignKeys [$ constraintKey ] = $ constraint ;
934+ assert (! isset ($ keysByName [$ constraintKey ]));
935+ $ foreignKeys [] = $ constraint ;
936+
937+ $ keysByName [$ constraintKey ] = count ($ foreignKeys ) - 1 ;
921938 } else {
922939 $ foreignKeys [] = $ constraint ;
923940 }
0 commit comments