diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index fcbb7b884..764d236a0 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -285,12 +285,15 @@ extension SQLRelation: Refinable { func withStableOrder() -> Self { with { relation in relation.ordering = relation.ordering.appending(Ordering(orderings: { [relation] db in - if try db.tableExists(source.tableName) { + do { + #warning("TODO: test with views") // Order by primary key. Don't order by rowid because those are // not stable: rowids can change after a vacuum. return try db.primaryKey(source.tableName).columns.map { SQLExpression.column($0).sqlOrdering } - } else { - // Support for views: create a stable order from all columns: + } catch { + // Could not find the primary key. Assume we're ordering + // a view that is not customized with the schemaSource. + // Create a stable order from all columns: // ORDER BY 1, 2, 3, ... let columnCount = try SQLQueryGenerator(relation: relation).columnCount(db) return (1...columnCount).map { SQL(sql: $0.description).sqlOrdering }