sqlalchemy code example
Example 1: install sqlalchemy
pip install -U Flask-SQLAlchemy
Example 2: sql alchemy or
from sqlalchemy import or_
filter(or_(User.name == 'ed', User.name == 'wendy'))
Example 3: sqlalchemy flask query
>>> from yourapplication import User
>>> admin = User(username='admin', email='[email protected]')
>>> guest = User(username='guest', email='[email protected]')
Example 4: sqlalchemy _in array sqlite
session.query(MyUserClass).filter(MyUserClass.id.in_((123,456))).all()
Example 5: python sqlalchemy db.session use table name as string
class Model1(db.Model):
__tablename__ = 'table1'
class Model2(db.Model):
__tablename__ = 'table2'
def table_object(table_name):
tables_dict = {table.__tablename__: table for table in db.Model.__subclasses__()}
return tables_dict.get(table_name)
model_list = ['table1', 'table2']
for model in model_list:
some_var = db.session.query(table_object(table_name=model)).filter_by(name='something').all()