diff --git a/README.md b/README.md
index 3086586..6b96d67 100644
--- a/README.md
+++ b/README.md
@@ -77,6 +77,7 @@ Boolean values can be one of:
| Auto Vacuum | `_vacuum` |
- `0` \| `none`
- `1` \| `full`
- `2` \| `incremental`
| For more information see [PRAGMA auto_vacuum](https://www.sqlite.org/pragma.html#pragma_auto_vacuum) |
| Busy Timeout | `_busy_timeout` \| `_timeout` | `int` | Specify value for sqlite3_busy_timeout. For more information see [PRAGMA busy_timeout](https://www.sqlite.org/pragma.html#pragma_busy_timeout) |
| Case Sensitive LIKE | `_cslike` | `boolean` | For more information see [PRAGMA case_sensitive_like](https://www.sqlite.org/pragma.html#pragma_case_sensitive_like) |
+| Defer Foreign Keys | `_defer_foreign_keys` \| `_defer_fk` | `boolean` | For more information see [PRAGMA defer_foreign_keys](https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys) |
| Foreign Keys | `_foreign_keys` \| `_fk` | `boolean` | For more information see [PRAGMA foreign_keys](https://www.sqlite.org/pragma.html#pragma_foreign_keys) |
| Mutex Locking | `_mutex` | | Specify mutex mode. |
| Recursive Triggers | `_recursive_triggers` \| `_rt` | `boolean` | For more information see [PRAGMA recursive_triggers](https://www.sqlite.org/pragma.html#pragma_recursive_triggers) |
diff --git a/sqlite3.go b/sqlite3.go
index 4de18d6..18a1f51 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -810,6 +810,9 @@ func errorString(err Error) string {
// Default or disabled the LIKE operation is case-insensitive.
// When enabling this options behaviour of LIKE will become case-sensitive.
//
+// _defer_foreign_keys=Boolean | _defer_fk=Boolean
+// Defer Foreign Keys until outermost transaction is committed.
+//
// _foreign_keys=Boolean | _fk=Boolean
// Enable or disable enforcement of foreign keys.
//
@@ -836,6 +839,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
autoVacuum := -1
busyTimeout := 5000
caseSensitiveLike := -1
+ deferForeignKeys := -1
foreignKeys := -1
recursiveTriggers := -1
@@ -935,6 +939,28 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
}
}
+ // Defer Foreign Keys
+ //
+ // https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys
+ //
+ pkey = "" // Reset pkey
+ if _, ok := params["_defer_foreign_keys"]; ok {
+ pkey = "_defer_foreign_keys"
+ }
+ if _, ok := params["_defer_fk"]; ok {
+ pkey = "_defer_fk"
+ }
+ if val := params.Get(pkey); val != "" {
+ switch strings.ToLower(val) {
+ case "0", "no", "false", "off":
+ deferForeignKeys = 0
+ case "1", "yes", "true", "on":
+ deferForeignKeys = 1
+ default:
+ return nil, fmt.Errorf("Invalid _foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
+ }
+ }
+
// Foreign Keys (_foreign_keys)
//
// https://www.sqlite.org/pragma.html#pragma_foreign_keys
@@ -1028,6 +1054,14 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
}
}
+ // Defer Foreign Keys
+ if deferForeignKeys > -1 {
+ if err := exec(fmt.Sprintf("PRAGMA defer_foreign_keys = %d;", deferForeignKeys)); err != nil {
+ C.sqlite3_close_v2(db)
+ return nil, err
+ }
+ }
+
// Forgein Keys
if foreignKeys > -1 {
if err := exec(fmt.Sprintf("PRAGMA foreign_keys = %d;", foreignKeys)); err != nil {