Skip to content

Commit 0db03d5

Browse files
psarnaLucioFranco
authored andcommitted
query_analysis: allow defer_foreign_keys pragma on primary (#980)
This pragma is useful for turning off foreign keys as a oneshot operation within current transaction. Example from turso shell: ``` → create table t(id); → create table t2(id, v references t(id)); ``` ``` -- First insert to t2 fails, because t has no row with value (6) → pragma foreign_keys=on; begin; insert into t2 values(5,6); insert into t values (6); commit; Error: SQLite error: FOREIGN KEY constraint failed ``` ``` -- First insert doesn't fail, because checking foreign keys is deferred -- until the transaction finishes → pragma foreign_keys=on; begin; pragma defer_foreign_keys=true; insert into t2 values(5,6); insert into t values (6); commit; OK ```
1 parent c6a0d59 commit 0db03d5

2 files changed

Lines changed: 2 additions & 4 deletions

File tree

libsql-server/src/query_analysis.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl StmtKind {
128128
// that already created a database, which is always the case for sqld
129129
"encoding" => Some(Self::Read),
130130
// always ok to be served by primary
131-
"foreign_keys" | "foreign_key_list" | "foreign_key_check" | "collation_list"
131+
"defer_foreign_keys" | "foreign_keys" | "foreign_key_list" | "foreign_key_check" | "collation_list"
132132
| "data_version" | "freelist_count" | "integrity_check" | "legacy_file_format"
133133
| "page_count" | "quick_check" | "stats" | "user_version" => Some(Self::Write),
134134
// ok to be served by primary without args
@@ -141,7 +141,6 @@ impl StmtKind {
141141
| "cache_spill"
142142
| "cell_size_check"
143143
| "checkpoint_fullfsync"
144-
| "defer_foreign_keys"
145144
| "fullfsync"
146145
| "hard_heap_limit"
147146
| "journal_mode"

libsql/src/parser.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl StmtKind {
131131
// that already created a database, which is always the case for sqld
132132
"encoding" => Some(Self::Read),
133133
// always ok to be served by primary
134-
"foreign_keys" | "foreign_key_list" | "foreign_key_check" | "collation_list"
134+
"defer_foreign_keys" | "foreign_keys" | "foreign_key_list" | "foreign_key_check" | "collation_list"
135135
| "data_version" | "freelist_count" | "integrity_check" | "legacy_file_format"
136136
| "page_count" | "quick_check" | "stats" | "user_version" => Some(Self::Write),
137137
// ok to be served by primary without args
@@ -144,7 +144,6 @@ impl StmtKind {
144144
| "cache_spill"
145145
| "cell_size_check"
146146
| "checkpoint_fullfsync"
147-
| "defer_foreign_keys"
148147
| "fullfsync"
149148
| "hard_heap_limit"
150149
| "journal_mode"

0 commit comments

Comments
 (0)