diff --git a/Documentation/Migrations.md b/Documentation/Migrations.md index 9b870923a..7831f3b6c 100644 --- a/Documentation/Migrations.md +++ b/Documentation/Migrations.md @@ -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) } diff --git a/GRDB/Core/Database+Schema.swift b/GRDB/Core/Database+Schema.swift index 04e2414dd..161d967c0 100644 --- a/GRDB/Core/Database+Schema.swift +++ b/GRDB/Core/Database+Schema.swift @@ -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 { diff --git a/Tests/GRDBTests/DatabaseMigratorTests.swift b/Tests/GRDBTests/DatabaseMigratorTests.swift index 196f08e05..acfa29399 100644 --- a/Tests/GRDBTests/DatabaseMigratorTests.swift +++ b/Tests/GRDBTests/DatabaseMigratorTests.swift @@ -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. diff --git a/Tests/GRDBTests/ForeignKeyInfoTests.swift b/Tests/GRDBTests/ForeignKeyInfoTests.swift index ab8cb362d..483db5179 100644 --- a/Tests/GRDBTests/ForeignKeyInfoTests.swift +++ b/Tests/GRDBTests/ForeignKeyInfoTests.swift @@ -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)") } } }