mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Distinguish "FOREIGN KEY constraint failed" from "FOREIGN KEY constraint violation" errors
This commit is contained in:
1 parent
127c4e0440
commit
c81a4dbf32
4 files changed
+14
-14
No files matched your search
@@ -227,7 +227,7 @@ In order to prevent foreign key violations from being committed to disk, you can
|
||||
In order to check for foreign key violations, the `checkForeignKeys()` and `checkForeignKeys(in:)` methods are recommended over the raw use of the [`PRAGMA foreign_key_check`](https://www.sqlite.org/pragma.html#pragma_foreign_key_check). Those methods throw a nicely detailed DatabaseError that contains a lot of debugging information:
|
||||
|
||||
```swift
|
||||
// SQLite error 19: foreign key constraint failed from player(teamId) to team(id),
|
||||
// SQLite error 19: FOREIGN KEY constraint violation - from player(teamId) to team(id),
|
||||
// in [id:1 teamId:2 name:"O'Brien" score:1000]
|
||||
try db.checkForeignKeys()
|
||||
```
|
||||
@@ -257,7 +257,7 @@ while let violation = try violations.next() {
|
||||
String(describing: violation)
|
||||
|
||||
// Rich description:
|
||||
// "foreign key constraint failed from player(teamId) to team(id),
|
||||
// "FOREIGN KEY constraint violation - from player(teamId) to team(id),
|
||||
// in [id:1 teamId:2 name:"O'Brien" score:1000]"
|
||||
try violation.failureDescription(db)
|
||||
}
|
||||
|
||||
@@ -808,7 +808,7 @@ public struct ForeignKeyViolation: FetchableRecord, CustomStringConvertible {
|
||||
|
||||
/// Returns a precise description of the foreign key violation.
|
||||
///
|
||||
/// For example: 'foreign key constraint failed from player(teamId) to team(id),
|
||||
/// For example: 'FOREIGN KEY constraint violation - from player(teamId) to team(id),
|
||||
/// in [id:1 teamId:2 name:"O'Brien" score: 1000]'
|
||||
public func failureDescription(_ db: Database) throws -> String {
|
||||
// Grab detailed information, if possible, for better error message
|
||||
@@ -822,12 +822,12 @@ public struct ForeignKeyViolation: FetchableRecord, CustomStringConvertible {
|
||||
var description: String
|
||||
if let foreignKey = foreignKey {
|
||||
description = """
|
||||
foreign key constraint failed \
|
||||
FOREIGN KEY constraint violation - \
|
||||
from \(originTable)(\(foreignKey.originColumns.joined(separator: ", "))) \
|
||||
to \(destinationTable)(\(foreignKey.destinationColumns.joined(separator: ", ")))
|
||||
"""
|
||||
} else {
|
||||
description = "foreign key constraint failed from \(originTable) to \(destinationTable)"
|
||||
description = "FOREIGN KEY constraint violation - from \(originTable) to \(destinationTable)"
|
||||
}
|
||||
|
||||
if let originRow = originRow {
|
||||
|
||||
@@ -400,7 +400,7 @@ class DatabaseMigratorTests : GRDBTestCase {
|
||||
|
||||
XCTAssertEqual(error.extendedResultCode, .SQLITE_CONSTRAINT_FOREIGNKEY)
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_CONSTRAINT)
|
||||
XCTAssertEqual(error.message, #"foreign key constraint failed from pets(masterId) to persons(id), in [masterId:123 name:"Bobby"]"#)
|
||||
XCTAssertEqual(error.message, #"FOREIGN KEY constraint violation - from pets(masterId) to persons(id), in [masterId:123 name:"Bobby"]"#)
|
||||
|
||||
let names = try dbQueue.inDatabase { db in
|
||||
try String.fetchAll(db, sql: "SELECT name FROM persons")
|
||||
@@ -420,7 +420,7 @@ class DatabaseMigratorTests : GRDBTestCase {
|
||||
let error = error as! DatabaseError
|
||||
XCTAssertEqual(error.extendedResultCode, .SQLITE_CONSTRAINT_FOREIGNKEY)
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_CONSTRAINT)
|
||||
XCTAssertEqual(error.message, #"foreign key constraint failed from pets(masterId) to persons(id), in [masterId:123 name:"Bobby"]"#)
|
||||
XCTAssertEqual(error.message, #"FOREIGN KEY constraint violation - from pets(masterId) to persons(id), in [masterId:123 name:"Bobby"]"#)
|
||||
|
||||
let names = try! String.fetchAll(db, sql: "SELECT name FROM persons")
|
||||
XCTAssertEqual(names, ["Arthur"])
|
||||
@@ -463,7 +463,7 @@ class DatabaseMigratorTests : GRDBTestCase {
|
||||
|
||||
XCTAssertEqual(error.extendedResultCode, .SQLITE_CONSTRAINT_FOREIGNKEY)
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_CONSTRAINT)
|
||||
XCTAssertEqual(error.message, #"foreign key constraint failed from pets(masterId) to persons(id), in [masterId:123 name:"Bobby"]"#)
|
||||
XCTAssertEqual(error.message, #"FOREIGN KEY constraint violation - from pets(masterId) to persons(id), in [masterId:123 name:"Bobby"]"#)
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
// Arthur inserted (migration 1), Barbara (migration 3) not inserted.
|
||||
|
||||
@@ -191,8 +191,8 @@ class ForeignKeyInfoTests: GRDBTestCase {
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_CONSTRAINT)
|
||||
XCTAssertEqual(error.extendedResultCode, .SQLITE_CONSTRAINT_FOREIGNKEY)
|
||||
XCTAssertEqual(error.message, #"foreign key constraint failed from child(parentId) to parent(id), in [id:13 parentId:"1"]"#)
|
||||
XCTAssertEqual(error.description, #"SQLite error 19: foreign key constraint failed from child(parentId) to parent(id), in [id:13 parentId:"1"]"#)
|
||||
XCTAssertEqual(error.message, #"FOREIGN KEY constraint violation - from child(parentId) to parent(id), in [id:13 parentId:"1"]"#)
|
||||
XCTAssertEqual(error.description, #"SQLite error 19: FOREIGN KEY constraint violation - from child(parentId) to parent(id), in [id:13 parentId:"1"]"#)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,8 +209,8 @@ class ForeignKeyInfoTests: GRDBTestCase {
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_CONSTRAINT)
|
||||
XCTAssertEqual(error.extendedResultCode, .SQLITE_CONSTRAINT_FOREIGNKEY)
|
||||
XCTAssertEqual(error.message, #"foreign key constraint failed from child(parentA, parentB) to parent(a, b), in [id:13 parentA:"foo" parentB:"bar"]"#)
|
||||
XCTAssertEqual(error.description, #"SQLite error 19: foreign key constraint failed from child(parentA, parentB) to parent(a, b), in [id:13 parentA:"foo" parentB:"bar"]"#)
|
||||
XCTAssertEqual(error.message, #"FOREIGN KEY constraint violation - from child(parentA, parentB) to parent(a, b), in [id:13 parentA:"foo" parentB:"bar"]"#)
|
||||
XCTAssertEqual(error.description, #"SQLite error 19: FOREIGN KEY constraint violation - from child(parentA, parentB) to parent(a, b), in [id:13 parentA:"foo" parentB:"bar"]"#)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,8 +227,8 @@ class ForeignKeyInfoTests: GRDBTestCase {
|
||||
} catch let error as DatabaseError {
|
||||
XCTAssertEqual(error.resultCode, .SQLITE_CONSTRAINT)
|
||||
XCTAssertEqual(error.extendedResultCode, .SQLITE_CONSTRAINT_FOREIGNKEY)
|
||||
XCTAssertEqual(error.message, "foreign key constraint failed from child(parentA, parentB) to parent(a, b)")
|
||||
XCTAssertEqual(error.description, "SQLite error 19: foreign key constraint failed from child(parentA, parentB) to parent(a, b)")
|
||||
XCTAssertEqual(error.message, "FOREIGN KEY constraint violation - from child(parentA, parentB) to parent(a, b)")
|
||||
XCTAssertEqual(error.description, "SQLite error 19: FOREIGN KEY constraint violation - from child(parentA, parentB) to parent(a, b)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user