From 1239178beed709a1407007ad45b92adec2df7ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 14 Jan 2024 14:36:26 +0100 Subject: [PATCH] Improve diagnostic when an association to/from view are lacking an explicit ForeignKey Related discussion: https://github.com/groue/GRDB.swift/discussions/1481 --- .../SQL/SQLForeignKeyRequest.swift | 114 +++++++++++++----- 1 file changed, 85 insertions(+), 29 deletions(-) diff --git a/GRDB/QueryInterface/SQL/SQLForeignKeyRequest.swift b/GRDB/QueryInterface/SQL/SQLForeignKeyRequest.swift index 3dbe56009..f1b38d7b5 100644 --- a/GRDB/QueryInterface/SQL/SQLForeignKeyRequest.swift +++ b/GRDB/QueryInterface/SQL/SQLForeignKeyRequest.swift @@ -32,41 +32,76 @@ struct SQLForeignKeyRequest { } // Incomplete information: let's look for schema foreign keys - let foreignKeys = try db.foreignKeys(on: originTable).filter { foreignKey in - if destinationTable.lowercased() != foreignKey.destinationTable.lowercased() { - return false - } - if let originColumns { - let originColumns = Set(originColumns.lazy.map { $0.lowercased() }) - let foreignKeyColumns = Set(foreignKey.mapping.lazy.map { $0.origin.lowercased() }) - if originColumns != foreignKeyColumns { - return false - } - } - if let destinationColumns { - // TODO: test - let destinationColumns = Set(destinationColumns.lazy.map { $0.lowercased() }) - let foreignKeyColumns = Set(foreignKey.mapping.lazy.map { $0.destination.lowercased() }) - if destinationColumns != foreignKeyColumns { - return false - } - } - return true + // + // But maybe the tables are views. In this case, don't throw + // "no such table" error, because this is confusing for the user, + // as discovered in . + // Instead, we'll crash with a clear message. + + guard let originType = try tableType(db, for: originTable) else { + throw DatabaseError.noSuchTable(originTable) } - // Matching foreign key(s) found - if let foreignKey = foreignKeys.first { - if foreignKeys.count == 1 { - // Non-ambiguous - return foreignKey.mapping - } else { - // Ambiguous: can't choose - fatalError("Ambiguous foreign key from \(originTable) to \(destinationTable)") + if originType.isView { + if originColumns == nil { + fatalError(""" + Could not infer foreign key from '\(originTable)' \ + to '\(destinationTable)'. To fix this error, provide an \ + explicit `ForeignKey` in the association definition. + """) + } + } else { + let foreignKeys = try db.foreignKeys(on: originTable).filter { foreignKey in + if destinationTable.lowercased() != foreignKey.destinationTable.lowercased() { + return false + } + if let originColumns { + let originColumns = Set(originColumns.lazy.map { $0.lowercased() }) + let foreignKeyColumns = Set(foreignKey.mapping.lazy.map { $0.origin.lowercased() }) + if originColumns != foreignKeyColumns { + return false + } + } + if let destinationColumns { + // TODO: test + let destinationColumns = Set(destinationColumns.lazy.map { $0.lowercased() }) + let foreignKeyColumns = Set(foreignKey.mapping.lazy.map { $0.destination.lowercased() }) + if destinationColumns != foreignKeyColumns { + return false + } + } + return true + } + + // Matching foreign key(s) found + if let foreignKey = foreignKeys.first { + if foreignKeys.count == 1 { + // Non-ambiguous + return foreignKey.mapping + } else { + // Ambiguous: can't choose + fatalError(""" + Ambiguous foreign key from '\(originTable)' to \ + '\(destinationTable)'. To fix this error, provide an \ + explicit `ForeignKey` in the association definition. + """) + } } } // No matching foreign key found: use the destination primary key if let originColumns { + guard let destinationType = try tableType(db, for: destinationTable) else { + throw DatabaseError.noSuchTable(destinationTable) + } + if destinationType.isView { + fatalError(""" + Could not infer foreign key from '\(originTable)' \ + to '\(destinationTable)'. To fix this error, provide an \ + explicit `ForeignKey` in the association definition, \ + with both origin and destination columns. + """) + } let destinationColumns = try db.primaryKey(destinationTable).columns if originColumns.count == destinationColumns.count { let mapping = zip(originColumns, destinationColumns).map { @@ -76,7 +111,28 @@ struct SQLForeignKeyRequest { } } - fatalError("Could not infer foreign key from \(originTable) to \(destinationTable)") + fatalError(""" + Could not infer foreign key from '\(originTable)' to \ + '\(destinationTable)'. To fix this error, provide an \ + explicit `ForeignKey` in the association definition. + """) + } + + private struct TableType { + var isView: Bool + } + + private func tableType(_ db: Database, for name: String) throws -> TableType? { + for schemaID in try db.schemaIdentifiers() { + if try db.schema(schemaID).containsObjectNamed(name, ofType: .table) { + return TableType(isView: false) + } + if try db.schema(schemaID).containsObjectNamed(name, ofType: .view) { + return TableType(isView: true) + } + } + + return nil } }