Skip to content

Commit 616e5bd

Browse files
committed
https://sqlite.org/pragma.html#pragma_defer_foreign_keys
“The defer_foreign_keys pragma is automatically switched off at each COMMIT or ROLLBACK. Hence, the defer_foreign_keys pragma must be separately enabled for each transaction. This pragma is only meaningful if foreign key constraints are enabled, of course.”
1 parent 7cc98a8 commit 616e5bd

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Tests/SQLiteTests/ConnectionTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,18 @@ class ConnectionTests : SQLiteTestCase {
159159
// This test case needs to emulate an environment where the individual statements succeed, but committing the
160160
// transaction fails. Using deferred foreign keys is one option to achieve this.
161161
try! db.execute("PRAGMA foreign_keys = ON;")
162-
try! db.execute("PRAGMA defer_foreign_keys = ON;")
163162
let stmt = try! db.prepare("INSERT INTO users (email, manager_id) VALUES (?, ?)", "alice@example.com", 100)
164163

165164
do {
166165
try db.transaction {
166+
try db.execute("PRAGMA defer_foreign_keys = ON;")
167167
try stmt.run()
168168
}
169-
} catch {
169+
XCTFail("expected error")
170+
} catch let Result.error(_, code, _) {
171+
XCTAssertEqual(SQLITE_CONSTRAINT, code)
172+
} catch let error {
173+
XCTFail("unexpected error: \(error)")
170174
}
171175

172176
AssertSQL("BEGIN DEFERRED TRANSACTION")

0 commit comments

Comments
 (0)