Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct column order for composite foreign keys #4978

Merged
merged 1 commit into from Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Platforms/MySQLPlatform.php
Expand Up @@ -185,7 +185,7 @@ public function getListTableForeignKeysSQL($table, $database = null)
}

$sql = 'SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ' .
'k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ ' .
'k.`REFERENCED_COLUMN_NAME`, k.`ORDINAL_POSITION` /*!50116 , c.update_rule, c.delete_rule */ ' .
morozov marked this conversation as resolved.
Show resolved Hide resolved
'FROM information_schema.key_column_usage k /*!50116 ' .
'INNER JOIN information_schema.referential_constraints c ON ' .
' c.constraint_name = k.constraint_name AND ' .
Expand All @@ -195,7 +195,8 @@ public function getListTableForeignKeysSQL($table, $database = null)

return $sql . ' AND k.table_schema = ' . $databaseNameSql
. ' /*!50116 AND c.constraint_schema = ' . $databaseNameSql . ' */'
. ' AND k.`REFERENCED_COLUMN_NAME` is not NULL';
. ' AND k.`REFERENCED_COLUMN_NAME` is not NULL'
. ' ORDER BY k.`ORDINAL_POSITION`';
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Platforms/SQLServer2012Platform.php
Expand Up @@ -1010,7 +1010,8 @@ public function getListTableForeignKeysSQL($table, $database = null)
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
ON f.OBJECT_ID = fc.constraint_object_id
WHERE ' .
$this->getTableWhereClause($table, 'SCHEMA_NAME (f.schema_id)', 'OBJECT_NAME (f.parent_object_id)');
$this->getTableWhereClause($table, 'SCHEMA_NAME (f.schema_id)', 'OBJECT_NAME (f.parent_object_id)') .
' ORDER BY fc.constraint_column_id';
AndreasA marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
49 changes: 49 additions & 0 deletions tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Expand Up @@ -30,6 +30,7 @@
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\TextType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;

use function array_filter;
use function array_keys;
Expand Down Expand Up @@ -1418,6 +1419,54 @@ public function testCommentInTable(): void
$table = $this->schemaManager->listTableDetails('table_with_comment');
self::assertSame('Foo with control characters \'\\', $table->getComment());
}

public function testCreatedCompositeForeignKeyOrderIsCorrectAfterCreation(): void
{
if (! $this->schemaManager->getDatabasePlatform()->supportsForeignKeyConstraints()) {
self::markTestSkipped('Platform does not support foreign keys.');
}

$foreignKey = 'fk_test_order';
$localTable = 'test_table_foreign';
$foreignTable = 'test_table_local';
$localColumns = ['child_col2', 'child_col1'];
$foreignColumns = ['col2', 'col1'];

$this->schemaManager->tryMethod('dropTable', $foreignTable);
$this->schemaManager->tryMethod('dropTable', $localTable);

$table = new Table($localTable);

$table->addColumn('col1', Types::INTEGER);
$table->addColumn('col2', Types::INTEGER);
$table->setPrimaryKey($foreignColumns);

$this->schemaManager->createTable($table);

$table = new Table($foreignTable);

$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
$table->addColumn('child_col1', Types::INTEGER);
$table->addColumn('child_col2', Types::INTEGER);
$table->setPrimaryKey(['id']);

$table->addForeignKeyConstraint(
$localTable,
$localColumns,
$foreignColumns,
[],
$foreignKey
);

$this->schemaManager->createTable($table);

$table = $this->schemaManager->listTableDetails($foreignTable);

$foreignKey = $table->getForeignKey($foreignKey);

self::assertSame($localColumns, array_map('strtolower', $foreignKey->getLocalColumns()));
self::assertSame($foreignColumns, array_map('strtolower', $foreignKey->getForeignColumns()));
}
}

interface ListTableColumnsDispatchEventListener
Expand Down