This repository was archived by the owner on Jun 5, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change 8
8
from alembic import command as alembic_command
9
9
from alembic .config import Config as AlembicConfig
10
10
from pydantic import BaseModel
11
- from sqlalchemy import CursorResult , TextClause , text
11
+ from sqlalchemy import CursorResult , TextClause , event , text
12
+ from sqlalchemy .engine import Engine
12
13
from sqlalchemy .exc import IntegrityError , OperationalError
13
14
from sqlalchemy .ext .asyncio import create_async_engine
14
15
@@ -35,6 +36,20 @@ class AlreadyExistsError(Exception):
35
36
pass
36
37
37
38
39
+ @event .listens_for (Engine , "connect" )
40
+ def set_sqlite_pragma (dbapi_connection , connection_record ):
41
+ """
42
+ Ensures that foreign keys are enabled for the SQLite database at every connection.
43
+ SQLite does not enforce foreign keys by default, so we need to enable them manually.
44
+ [SQLAlchemy docs](https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#foreign-key-support)
45
+ [SQLite docs](https://www.sqlite.org/foreignkeys.html)
46
+ [SO](https://stackoverflow.com/questions/2614984/sqlite-sqlalchemy-how-to-enforce-foreign-keys)
47
+ """
48
+ cursor = dbapi_connection .cursor ()
49
+ cursor .execute ("PRAGMA foreign_keys=ON" )
50
+ cursor .close ()
51
+
52
+
38
53
class DbCodeGate :
39
54
_instance = None
40
55
You can’t perform that action at this time.
0 commit comments