Skip to content

Commit 715d10a

Browse files
committed
fix sqlite fk handling in tests
1 parent a128dac commit 715d10a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/common.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from os import getenv
22
from pathlib import Path
33

4+
from sqlalchemy import event
5+
from sqlalchemy.engine import Engine
6+
47

58
def sqla_uri():
69
testing_db_url = getenv("TESTING_DB_URL")
@@ -12,3 +15,18 @@ def sqla_uri():
1215

1316
def is_postgres_tests() -> bool:
1417
return "postgres" in sqla_uri()
18+
19+
20+
def is_sqlite_tests() -> bool:
21+
return "sqlite" in sqla_uri()
22+
23+
24+
@event.listens_for(Engine, "connect")
25+
def set_sqlite_pragma(dbapi_connection, connection_record):
26+
"""
27+
https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#foreign-key-support
28+
"""
29+
if is_sqlite_tests():
30+
cursor = dbapi_connection.cursor()
31+
cursor.execute("PRAGMA foreign_keys=ON")
32+
cursor.close()

0 commit comments

Comments
 (0)