Enable SQL foreign key support for SQLite

This commit is contained in:
grossmj committed 2021-05-06 17:21:45 +09:30
1 parent a1f3f6472a
commit 10164e262d
3 files changed
+13 -3

No files matched your search

+12
View File
@@ -22,6 +22,8 @@ from fastapi.encoders import jsonable_encoder
from pydantic import ValidationError
from typing import List
from sqlalchemy import event
from sqlalchemy.engine import Engine
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from gns3server.db.repositories.computes import ComputesRepository
@@ -49,6 +51,16 @@ async def connect_to_db(app: FastAPI) -> None:
log.error(f"Error while connecting to database '{db_url}: {e}")
@event.listens_for(Engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
# Enable SQL foreign key support for SQLite
# https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#foreign-key-support
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.close()
async def get_computes(app: FastAPI) -> List[dict]:
computes = []