[BangPypers] Flask-SQLAlchemy and SQLite

Balachandran Sivakumar benignbala at gmail.com
Thu Jul 28 09:50:54 EDT 2016


Hi,

    I am working on creating a website using Flask. We are using the
Flask-SQLAlchemy extension for ORM and SQLite for DB.

SQLIte doesn't have foreign key constraints enabled by default. They have
to be enabled using the

PRAGMA foreign_keys = ON;

directive on each connection. I am not able to get flask-sqlalchemy to
issue this directive on each connect. This is what I am doing in my
"database.py" which is imported by all models subsequently.


from flask_sqlalchemy import SQLAlchemy, event
from mysite import app


def enable_fkey(dbcon, con_rec):
    dbcon.execute('PRAGMA foreign_keys=ON;')

db = SQLAlchemy(app)
event.listen(db.get_engine(app), 'connect', enable_fkey)

Can someone please help me with turning on SQLite Foreign Key constraints
on each db connection ? Thanks


-- 
Thank you
Balachandran Sivakumar


More information about the BangPypers mailing list