Skip to content

Commit 3e9159e

Browse files
EdaCZdg
authored andcommitted
Structure: foreign keys with less columns have bigger priority (#170)
1 parent 08d9140 commit 3e9159e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/Database/Structure.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,19 @@ protected function analyzePrimaryKey(array $columns)
243243
protected function analyzeForeignKeys(array &$structure, string $table): void
244244
{
245245
$lowerTable = strtolower($table);
246-
foreach ($this->connection->getSupplementalDriver()->getForeignKeys($table) as $row) {
246+
247+
$foreignKeys = $this->connection->getSupplementalDriver()->getForeignKeys($table);
248+
249+
$fksColumnsCounts = [];
250+
foreach ($foreignKeys as $foreignKey) {
251+
$tmp = &$fksColumnsCounts[$foreignKey['name']];
252+
$tmp++;
253+
}
254+
usort($foreignKeys, function ($a, $b) use ($fksColumnsCounts): int {
255+
return $fksColumnsCounts[$b['name']] <=> $fksColumnsCounts[$a['name']];
256+
});
257+
258+
foreach ($foreignKeys as $row) {
247259
$structure['belongsTo'][$lowerTable][$row['local']] = $row['table'];
248260
$structure['hasMany'][strtolower($row['table'])][$table][] = $row['local'];
249261
}

0 commit comments

Comments
 (0)