Skip to content

Commit 2ac14ae

Browse files
committed
Use SQLAlchemy's event.listens_for() to ensure that foreign key constraint is enforced on new connections before use.
1 parent 28603d7 commit 2ac14ae

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

stix2/datastore/relational_db/database_backends/sqlite_backend.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import Any
33

44
from sqlalchemy import TIMESTAMP, LargeBinary, Text
5-
from sqlalchemy.engine import Engine
65
from sqlalchemy import event
76

87
from stix2.base import (
@@ -19,11 +18,14 @@ class SQLiteBackend(DatabaseBackend):
1918
def __init__(self, database_connection_url=default_database_connection_url, force_recreate=False, **kwargs: Any):
2019
super().__init__(database_connection_url, force_recreate=force_recreate, **kwargs)
2120

22-
set_sqlite_pragma(self)
23-
24-
@event.listens_for(Engine, "connect")
25-
def set_sqlite_pragma(self):
26-
self.database_connection.execute("PRAGMA foreign_keys=ON")
21+
@event.listens_for(self.database_connection, "connect")
22+
def set_sqlite_pragma(dbapi_connection, connection_record):
23+
cursor = dbapi_connection.cursor()
24+
cursor.execute("PRAGMA foreign_keys=ON")
25+
result = cursor.execute("PRAGMA foreign_keys")
26+
for row in result:
27+
print('PRAGMA foreign_keys:', row)
28+
cursor.close()
2729

2830
# =========================================================================
2931
# sql type methods (overrides)

0 commit comments

Comments
 (0)