Skip to content

Instantly share code, notes, and snippets.

@lucidfrontier45
Created January 27, 2015 09:11
Show Gist options
  • Save lucidfrontier45/7a4989490300d1298ba0 to your computer and use it in GitHub Desktop.
Save lucidfrontier45/7a4989490300d1298ba0 to your computer and use it in GitHub Desktop.
Enforce FK constraint for SQLite with when using flask-sqlalchemy
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
try:
app.config.from_object("config")
except:
pass
if app.config["SQLALCHEMY_DATABASE_URI"].startswith("sqlite"):
def _fk_pragma_on_connect(dbapi_con, con_record):
dbapi_con.execute('pragma foreign_keys=ON')
from sqlalchemy import event
event.listen(sensing_db.engine, 'connect', _fk_pragma_on_connect)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment