IBM / dse-decision-optimization-utilities

A Python package with various utilities that speed-up development of Decision Optimization projects in Watson Studio. Implements common tasks and provides best-practice guidelines to architect DO projects.
Apache License 2.0
4 stars 7 forks source link

ScenarioDbManager - SQLite enable FK checks #29

Closed victorterpstra closed 2 years ago

victorterpstra commented 2 years ago

SQLite doesn't automatically check for FK violations. Currently, we need to do the below in a notebook cell. Find a way to incorporate in the package/class

from sqlalchemy import event
from sqlalchemy.engine import Engine
from sqlite3 import Connection as SQLite3Connection

@event.listens_for(Engine, "connect")
def _set_sqlite_pragma(dbapi_connection, connection_record):
    if isinstance(dbapi_connection, SQLite3Connection):
        cursor = dbapi_connection.cursor()
        cursor.execute("PRAGMA foreign_keys=ON;")
        cursor.close()
victorterpstra commented 2 years ago

Fixed and tested in v0.5.3.0b Added a parameter enable_sqlite_fk to the constructor. The above code is run dynamically just before the SQLite engine is created. Seems to be able to dynamically enable/disable the FK checks in SQLite, i.e. creating a new ScenarioDbManager with a different setting. Has no impact on how DB2WH is checking FK constraints.