diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 61ebf1c92..296124551 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -500,6 +500,20 @@ struct SQLQueryGenerator: Refinable { return .none } + // Grouping something which is not a table: assume non unique grouping. + guard case let .table(tableName: tableName, alias: alias) = relation.source else { + return .nonUnique + } + + // FastPrimaryKeyExpression? + for expression in groupExpressions { + if + let column = expression as? QualifiedFastPrimaryKeyExpression, + column.alias == alias { + return .unique + } + } + // Grouping by something which is not a column: assume non // unique grouping. // SELECT * FROM player GROUP BY (score + bonus) @@ -508,11 +522,6 @@ struct SQLQueryGenerator: Refinable { return .nonUnique } - // Grouping something which is not a table: assume non unique grouping. - guard case let .table(tableName: tableName, alias: alias) = relation.source else { - return .nonUnique - } - // Grouping by some column(s) which do not come from the main table: // assume non unique grouping. // SELECT * FROM player JOIN team ... GROUP BY team.id