ForeignKeyViolation.databaseError(_:)

This commit is contained in:
Gwendal Roué committed 2021-11-16 08:51:33 +01:00
1 parent ebc8524706
commit 93c4249619
2 files changed
+15 -9

No files matched your search

+3
View File
@@ -260,6 +260,9 @@ while let violation = try violations.next() {
// "FOREIGN KEY constraint violation - from player(teamId) to team(id),
// in [id:1 teamId:2 name:"O'Brien" score:1000]"
try violation.failureDescription(db)
// Turn violation into a DatabaseError
throw violation.databaseError(db)
}
```
+12 -9
View File
@@ -496,16 +496,9 @@ extension Database {
}
private func checkForeignKeys(from violations: RecordCursor<ForeignKeyViolation>) throws {
guard let violation = try violations.next() else {
return
if let violation = try violations.next() {
throw violation.databaseError(self)
}
// Grab detailed information, if possible, for better error message.
// If detailed information is not available, fallback to plain description.
let message = (try? violation.failureDescription(self)) ?? String(describing: violation)
throw DatabaseError(
resultCode: .SQLITE_CONSTRAINT_FOREIGNKEY,
message: message)
}
/// Returns the actual name of the database table, in the main or temp
@@ -843,6 +836,16 @@ public struct ForeignKeyViolation: FetchableRecord, CustomStringConvertible {
return description
}
/// Returns a DatabaseError of extended code `SQLITE_CONSTRAINT_FOREIGNKEY`
public func databaseError(_ db: Database) -> DatabaseError {
// Grab detailed information, if possible, for better error message.
// If detailed information is not available, fallback to plain description.
let message = (try? failureDescription(db)) ?? String(describing: self)
return DatabaseError(
resultCode: .SQLITE_CONSTRAINT_FOREIGNKEY,
message: message)
}
}
/// Primary keys are returned from the Database.primaryKey(_:) method.