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 +17
-1
lines changed
Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change 88from alembic import command as alembic_command
99from alembic .config import Config as AlembicConfig
1010from pydantic import BaseModel
11- from sqlalchemy import CursorResult , TextClause , text
11+ from sqlalchemy import CursorResult , TextClause , event , text
12+ from sqlalchemy .engine import Engine
1213from sqlalchemy .exc import IntegrityError , OperationalError
1314from sqlalchemy .ext .asyncio import create_async_engine
1415
@@ -35,6 +36,21 @@ class AlreadyExistsError(Exception):
3536 pass
3637
3738
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+
44+ SQLite does not enforce foreign keys by default, so we need to enable them manually.
45+ [SQLAlchemy docs](https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#foreign-key-support)
46+ [SQLite docs](https://www.sqlite.org/foreignkeys.html)
47+ [SO](https://stackoverflow.com/questions/2614984/sqlite-sqlalchemy-how-to-enforce-foreign-keys)
48+ """
49+ cursor = dbapi_connection .cursor ()
50+ cursor .execute ("PRAGMA foreign_keys=ON" )
51+ cursor .close ()
52+
53+
3854class DbCodeGate :
3955 _instance = None
4056
You can’t perform that action at this time.
0 commit comments