Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Tools/Toolkits/MySQL/MySQLSchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ protected function getRelationships(): array
$params = array_merge($this->tables, $this->tables);
}

// ORDINAL_POSITION must appear in the SELECT list: MySQL/MariaDB with
// ONLY_FULL_GROUP_BY reject DISTINCT + ORDER BY on non-selected columns.
$stmt = $this->pdo->prepare("
SELECT DISTINCT
kcu.CONSTRAINT_NAME,
Expand All @@ -245,7 +247,8 @@ protected function getRelationships(): array
kcu.REFERENCED_TABLE_NAME as target_table,
kcu.REFERENCED_COLUMN_NAME as target_column,
rc.UPDATE_RULE,
rc.DELETE_RULE
rc.DELETE_RULE,
kcu.ORDINAL_POSITION
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
ON kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
Expand Down
12 changes: 12 additions & 0 deletions tests/Tools/Toolkits/MySQL/MySQLSchemaToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ public function test_lookups_are_scoped_to_the_current_database(): void
}
}

public function test_relationships_query_selects_ordinal_position_for_strict_sql_mode(): void
{
(new MySQLSchemaTool($this->informationSchema()))();

$sql = $this->lookup('KEY_COLUMN_USAGE')['sql'];

$this->assertMatchesRegularExpression(
'/SELECT DISTINCT\b[\s\S]*\bkcu\.ORDINAL_POSITION\b[\s\S]*\bORDER BY\b[\s\S]*\bkcu\.ORDINAL_POSITION\b/i',
$sql
);
}

public function test_tool_takes_no_input(): void
{
$tool = new MySQLSchemaTool($this->createMock(PDO::class));
Expand Down
Loading