Better foreign key enablement
This commit is contained in:
parent
a08257ec76
commit
f9b14a7622
1 changed files with 6 additions and 5 deletions
|
|
@ -90,11 +90,12 @@ class Database:
|
||||||
def __init__(self, path: pathlib.Path | str, origin: str):
|
def __init__(self, path: pathlib.Path | str, origin: str):
|
||||||
if not isinstance(path, str):
|
if not isinstance(path, str):
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
db = sqlite3.Connection(str(path), autocommit=False)
|
|
||||||
# This is WILD that I need to do this because you cannot enable or
|
# Enable autocommit as a separate step so that I can enable foreign
|
||||||
# disable foreign keys with a transaction active, and
|
# keys cleanly. (Can't enable foreign keys in a transaction.)
|
||||||
# `autocommit=False` means that a transaction is *always* active.
|
db = sqlite3.Connection(str(path))
|
||||||
db.executescript("COMMIT;PRAGMA foreign_keys = ON;BEGIN;")
|
db.execute("PRAGMA foreign_keys = ON")
|
||||||
|
db.autocommit = False
|
||||||
|
|
||||||
cursor = db.execute("PRAGMA foreign_keys")
|
cursor = db.execute("PRAGMA foreign_keys")
|
||||||
rows = cursor.fetchall()
|
rows = cursor.fetchall()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue