From 74a15171f40378655f4869799ff09826956ba67e Mon Sep 17 00:00:00 2001 From: Ciro Date: Sun, 11 Oct 2026 12:50:07 +0200 Subject: [PATCH] fix MySQLSchemaTool FK query for ONLY_FULL_GROUP_BY Include ORDINAL_POSITION in SELECT DISTINCT so MySQL/MariaDB accept ORDER BY under strict SQL mode. --- src/Tools/Toolkits/MySQL/MySQLSchemaTool.php | 5 ++++- tests/Tools/Toolkits/MySQL/MySQLSchemaToolTest.php | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Tools/Toolkits/MySQL/MySQLSchemaTool.php b/src/Tools/Toolkits/MySQL/MySQLSchemaTool.php index 94aba232..3e559045 100644 --- a/src/Tools/Toolkits/MySQL/MySQLSchemaTool.php +++ b/src/Tools/Toolkits/MySQL/MySQLSchemaTool.php @@ -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, @@ -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 diff --git a/tests/Tools/Toolkits/MySQL/MySQLSchemaToolTest.php b/tests/Tools/Toolkits/MySQL/MySQLSchemaToolTest.php index bc7a0581..e7a7251c 100644 --- a/tests/Tools/Toolkits/MySQL/MySQLSchemaToolTest.php +++ b/tests/Tools/Toolkits/MySQL/MySQLSchemaToolTest.php @@ -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));