0

My Flask app should execute some rather complicated sql queries, parse them and display results. Now I'm using SQLAlchemy (as described here), but I want to switch to Records, simple wrapper over SQLAlchemy (it has all I need out of the box). I haven't found any examples of integration Records into Flask.

Can you provide an example of simple Flask app with vanilla Records integration?

Community
  • 1
  • 1
kkoommbb
  • 15
  • 2

1 Answers1

2

Records handle the SQLAlchemy connection lifecycle, I don't think it is a good idea to really integrate them. You can share the config, passing the database URL to records.Database.

app = Flask(__name__)
app.config['DATABASE_URL'] = 'postgres://...'
records_db = records.Database(app.config['DATABASE_URL'])

If you use Flask-SQLAlchemy, the config key is SQLALCHEMY_DATABASE_URI.

Add the records_db to Flask context g to access it from any file with a Flask context, without messing with circular imports.

from flask import g
g.records = records_db
iurisilvio
  • 4,868
  • 1
  • 30
  • 36