From 9518d74fa6883f1839a7dbb2a62fbb68e8740e29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 31 Aug 2025 13:09:16 +0200 Subject: [PATCH] Improve error message --- GRDB/Core/Database+Schema.swift | 33 ++++++++++++++++--- .../Request/RequestProtocols.swift | 25 +++++++++++--- Tests/GRDBTests/PrimaryKeyInfoTests.swift | 32 ++++++++++++++---- 3 files changed, 75 insertions(+), 15 deletions(-) diff --git a/GRDB/Core/Database+Schema.swift b/GRDB/Core/Database+Schema.swift index 8febc3767..570fdde97 100644 --- a/GRDB/Core/Database+Schema.swift +++ b/GRDB/Core/Database+Schema.swift @@ -347,7 +347,22 @@ extension Database { return result } } - throw DatabaseError.noSuchTable(tableName) + + if (try? viewExists(tableName, in: schemaName)) == true { + if configuration.schemaSource == nil { + throw DatabaseError(message: """ + The database view '\(tableName)' has no primary key. \ + To support views, provide a custom schema source in Configuration.schemaSource. + """) + } else { + throw DatabaseError(message: """ + The database view '\(tableName)' has no primary key, \ + according to Configuration.schemaSource. + """) + } + } else { + throw DatabaseError.noSuchTable(tableName) + } } /// Returns the name of the single-column primary key. @@ -370,10 +385,18 @@ extension Database { if case .SQLITE_ERROR = error.resultCode, (try? viewExists(tableName)) == true { - fatalError(""" - Filtering by primary key is not available on the database view '\(tableName)'. \ - Use `filter(Column("...") == value)` instead. - """) + if configuration.schemaSource == nil { + fatalError(""" + Filtering by primary key is not available on the database view '\(tableName)'. \ + Instead, use `filter(Column("...") == value)`, or provide \ + a custom schema source in Configuration.schemaSource. + """) + } else { + fatalError(""" + Filtering by primary key requires a single-column primary key in the view '\(tableName)'. \ + No such primary key is configured by Configuration.schemaSource. + """) + } } else { throw error } diff --git a/GRDB/QueryInterface/Request/RequestProtocols.swift b/GRDB/QueryInterface/Request/RequestProtocols.swift index 9e13293f6..9b88e64dd 100644 --- a/GRDB/QueryInterface/Request/RequestProtocols.swift +++ b/GRDB/QueryInterface/Request/RequestProtocols.swift @@ -826,10 +826,27 @@ extension TableRequest where Self: FilteredRequest, Self: TypedRequest { // ("foo", "bar") do not contain a unique key (primary key // or unique index). guard let columns = try db.columnsForUniqueKey(key.keys, in: databaseTableName) else { - fatalError(""" - table \(databaseTableName) has no unique key on column(s) \ - \(key.keys.sorted().joined(separator: ", ")) - """) + if (try? db.viewExists(databaseTableName)) == true { + if db.configuration.schemaSource == nil { + fatalError(""" + view \(databaseTableName) has no unique key on column(s) \ + \(key.keys.sorted().joined(separator: ", ")). To support \ + keys in views, provide a custom schema source in \ + Configuration.schemaSource. + """) + } else { + fatalError(""" + view \(databaseTableName) has no unique key on column(s) \ + \(key.keys.sorted().joined(separator: ", ")), \ + according to Configuration.schemaSource. + """) + } + } else { + fatalError(""" + table \(databaseTableName) has no unique key on column(s) \ + \(key.keys.sorted().joined(separator: ", ")) + """) + } } let lowercaseColumns = columns.map { $0.lowercased() } diff --git a/Tests/GRDBTests/PrimaryKeyInfoTests.swift b/Tests/GRDBTests/PrimaryKeyInfoTests.swift index ba66b2b94..25c9485ce 100644 --- a/Tests/GRDBTests/PrimaryKeyInfoTests.swift +++ b/Tests/GRDBTests/PrimaryKeyInfoTests.swift @@ -26,8 +26,16 @@ class PrimaryKeyInfoTests: GRDBTestCase { XCTFail("Expected Error") } catch let error as DatabaseError { XCTAssertEqual(error.resultCode, .SQLITE_ERROR) - XCTAssertEqual(error.message, "no such table: items") - XCTAssertEqual(error.description, "SQLite error 1: no such table: items") + XCTAssertEqual(error.message, """ + The database view 'items' has no primary key. \ + To support views, provide a custom schema source \ + in Configuration.schemaSource. + """) + XCTAssertEqual(error.description, """ + SQLite error 1: The database view 'items' has no primary key. \ + To support views, provide a custom schema source \ + in Configuration.schemaSource. + """) } } } @@ -273,8 +281,14 @@ class PrimaryKeyInfoTests: GRDBTestCase { XCTFail("Expected Error") } catch let error as DatabaseError { XCTAssertEqual(error.resultCode, .SQLITE_ERROR) - XCTAssertEqual(error.message, "no such table: playerView") - XCTAssertEqual(error.description, "SQLite error 1: no such table: playerView") + XCTAssertEqual(error.message, """ + The database view 'playerView' has no primary key, \ + according to Configuration.schemaSource. + """) + XCTAssertEqual(error.description, """ + SQLite error 1: The database view 'playerView' has no primary key, \ + according to Configuration.schemaSource. + """) } } } @@ -307,8 +321,14 @@ class PrimaryKeyInfoTests: GRDBTestCase { XCTFail("Expected Error") } catch let error as DatabaseError { XCTAssertEqual(error.resultCode, .SQLITE_ERROR) - XCTAssertEqual(error.message, "no such table: playerView") - XCTAssertEqual(error.description, "SQLite error 1: no such table: playerView") + XCTAssertEqual(error.message, """ + The database view 'playerView' has no primary key, \ + according to Configuration.schemaSource. + """) + XCTAssertEqual(error.description, """ + SQLite error 1: The database view 'playerView' has no primary key, \ + according to Configuration.schemaSource. + """) } } }