I'm trying out flask-sqlalchemy in a simple test app. I'm running a SQL server, and I can connect to from Flask Using SQLalchemy like so:
from flask import render_template
from app import app, db
@app.route('/')
@app.route('/index')
def index():
people = list(db.session.execute("select top 10 * from people where ppl_username IS NOT NULL"))
However, I would also like to use the SQL psuedo language and ORM part of SQLalchemy to query. Because this is an existing database I don't want to write my own classes and generate a database, I would like to reflect the existing database and access it that way. I have found the reflect method in the API docs, but I can't figure out how (and where) to use that.
Besides wondering how to do this; I'm also wondering:
Thanks,
Yes it is all possible. I use sqlautocode to do exactly what you are talking about. It generates sqlalchemy code to create the tables/column in sqlalchemy and places them in a file. Simply install it and then from the command line run it.
This generates the models sqlalchemy models from an existing mysql db for my webapp and creates a file alchemy_models.py:
sqlautocode mysql://<dbuser>:<pass>@localhost:3306/<dbname> -o alchemy_models.py
Note the mysql:// bit is merely the syntaxt to generate a connection in SA
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With