I have been working on a flask app that is database intensive for about 4 months. I am pretty new to python, flask and sqlalchemy but very familiar with web and database programming.
I mainly use core SQL for the hard database bits but have used the ORM as well. The main methods I use with core SQL are the text() and bindparam() functions to give me database independence via sqlalchemy dialects.
I have found myself having to intermix my module imports to get the stuff I want. Here is a simple but typical import.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import bindparam
from sqlalchemy.sql import text
The SQLAlchemy object then gets used in the ORM bits of my code and I believe it also handles sessions as per this. It is initialised something like this.
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = appdb
app.config['SQLALCHEMY_BINDS'] = {'meta': foodb, 'data': bardb}
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
I then use the db
variable to access all the flask_sqlalchemy stuff.
The direct sqlalchemy stuff gets used in my core SQL and looks something like this.
What I am finding now is that I am intermixing the code, for example like this.
eng = db.get_engine(bind='meta')
cxn = eng.connect()
sql = text('select * from foo')
rows = cxn.execute(sql).fetchall()
It seems to me that if I have decided to use flask_sqlalchemy I shouldn't have to import sqlalchemy stuff seperately, it should be somewhere in flask_sqlalchemy. Intermixing makes me nervous about side-effects.
So I have two questions.
- Is intermixing like this safe?
- Can I get text() and bindparam() and other sqlalchemy stuff from flask_sqlalchemy somehow? If so how?