-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ensure that sqlite databases enforce foreign key constraints (#432)
- Loading branch information
Showing
5 changed files
with
40 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from sqlalchemy import Pool | ||
from sqlalchemy.event import listen | ||
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine | ||
|
||
|
||
def create_engine(connection_string: str) -> AsyncEngine: | ||
# enforce foreign key constraints on SQLite: | ||
# https://docs.sqlalchemy.org/en/20/dialects/sqlite.html#foreign-key-support | ||
def set_sqlite_pragma(dbapi_connection, connection_record): | ||
cursor = dbapi_connection.cursor() | ||
cursor.execute("PRAGMA foreign_keys=ON") | ||
cursor.close() | ||
|
||
if connection_string.startswith("sqlite+"): | ||
listen(Pool, "connect", set_sqlite_pragma) | ||
|
||
return create_async_engine(connection_string, future=True, echo=False, pool_pre_ping=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters