Relative Content

Tag Archive for pythonsqlalchemy

SqlAlchemy: add more tables to select in existing statement

I have a bunch of tables in my database, that are all linked together in the same way. I need to run queries against that bunch of tables, but I would like to be able to figure out on the subset of those tables at runtime. To do the select, I need to list my tables in the select statement and then reference them in the join clauses:

About python loop and sqlalchemy

def getBeforePoint(userId): today = datetime.now() subquery = ( db.session.query( T_user_point.acquired_at, M_promotion_code.valid_days, T_user_promotion_code.promotion_code_id, T_user_point.user_promotion_code_id, T_user_promotion_code.user_promotion_code_id, ) .join(T_user_promotion_code, T_user_point.user_promotion_code_id == T_user_promotion_code.user_promotion_code_id) .join(M_promotion_code, T_user_promotion_code.promotion_code_id == M_promotion_code.promotion_code_id) .filter(T_user_point.user_id == userId) .subquery() ) result = db.session.query( subquery.c.acquired_at, subquery.c.valid_days ).all() loop_counter = 0 point_with_valid_days = 0 for row in result: acquired_at = row.acquired_at valid_days = row.valid_days if valid_days is not None: […]

Python and sqlalchemy composition

def getBeforePoint(userId): today = datetime.now() subquery = ( db.session.query( T_user_point.acquired_at, M_promotion_code.valid_days, T_user_promotion_code.promotion_code_id, T_user_point.user_promotion_code_id, T_user_promotion_code.user_promotion_code_id, ) .join(T_user_promotion_code, T_user_point.user_promotion_code_id == T_user_promotion_code.user_promotion_code_id) .join(M_promotion_code, T_user_promotion_code.promotion_code_id == M_promotion_code.promotion_code_id) .filter(T_user_point.user_id == userId) .subquery() ) result = db.session.query( subquery.c.acquired_at, subquery.c.valid_days ).all() loop_counter = 0 point_with_valid_days = 0 for row in result: acquired_at = row.acquired_at valid_days = row.valid_days if valid_days is not None: […]

Python and sqlalchemy structure

def getBeforePoint(userId): today = datetime.now() subquery = ( db.session.query( T_user_point.acquired_at, M_promotion_code.valid_days, T_user_promotion_code.promotion_code_id, T_user_point.user_promotion_code_id, T_user_promotion_code.user_promotion_code_id, ) .join(T_user_promotion_code, T_user_point.user_promotion_code_id == T_user_promotion_code.user_promotion_code_id) .join(M_promotion_code, T_user_promotion_code.promotion_code_id == M_promotion_code.promotion_code_id) .filter(T_user_point.user_id == userId) .subquery() ) result = db.session.query( subquery.c.acquired_at, subquery.c.valid_days ).all() loop_counter = 0 point_with_valid_days = 0 for row in result: acquired_at = row.acquired_at valid_days = row.valid_days if valid_days is not None: […]

Tracking updates in a SQLAlchemy Entity with before and after values

I have a SQLAlchemy entity that I’d like to track the “before” and “after” states on in an event capture. e.g. user.name was “robert”, and then it was updated to “bob”. In the example below, I only have access to “bob” (via sqlalchemy’s get_history) at the time of the event being triggered.

SQLAlchemy Case Expression In Filter

Is there a way to use the the in_ operator on a case expression. The below is a contrived example but I am looking to do something like this.