@@ -319,11 +319,13 @@ private function reverseEngineerMappingFromDatabase(): void
319319 */
320320 private function buildIndexes (ClassMetadata $ metadata ): void
321321 {
322- $ tableName = $ metadata ->table ['name ' ];
323- $ indexes = $ this ->tables [$ tableName ]->getIndexes ();
322+ $ tableName = $ metadata ->table ['name ' ];
323+ $ table = $ this ->tables [$ tableName ];
324+ $ primaryKey = self ::getPrimaryKey ($ table );
325+ $ indexes = $ table ->getIndexes ();
324326
325327 foreach ($ indexes as $ index ) {
326- if ($ index-> isPrimary () ) {
328+ if ($ index === $ primaryKey ) {
327329 continue ;
328330 }
329331
@@ -506,7 +508,7 @@ private function buildToOneAssociationMappings(ClassMetadata $metadata): void
506508 private function getTablePrimaryKeys (Table $ table ): array
507509 {
508510 try {
509- if (method_exists ($ table , 'getPrimaryKeyConstraints ' )) {
511+ if (method_exists ($ table , 'getPrimaryKeyConstraint ' )) {
510512 return array_map (static fn (UnqualifiedName $ name ) => $ name ->toString (), $ table ->getPrimaryKeyConstraint ()->getColumnNames ());
511513 }
512514
@@ -594,4 +596,27 @@ private static function getIndexedColumns(Index $index): array
594596
595597 return $ index ->getColumns ();
596598 }
599+
600+ private static function getPrimaryKey (Table $ table ): Index |null
601+ {
602+ $ primaryKeyConstraint = null ;
603+
604+ if (method_exists (Table::class, 'getPrimaryKeyConstraint ' )) {
605+ $ primaryKeyConstraint = $ table ->getPrimaryKeyConstraint ();
606+ }
607+
608+ foreach ($ table ->getIndexes () as $ index ) {
609+ if ($ primaryKeyConstraint !== null ) {
610+ $ primaryKeyConstraintColumns = array_map (static fn (UnqualifiedName $ name ) => $ name ->toString (), $ primaryKeyConstraint ->getColumnNames ());
611+
612+ if ($ primaryKeyConstraintColumns === self ::getIndexedColumns ($ index )) {
613+ return $ index ;
614+ }
615+ } elseif ($ index ->isPrimary ()) {
616+ return $ index ;
617+ }
618+ }
619+
620+ return null ;
621+ }
597622}
0 commit comments