I have the following two queries:
res=session.query(t_marketing_mailing_stats_tbl).filter(
t_marketing_mailing_stats_tbl.c.mailing_id==mid)
res_t=session.query(t_marketing_time_stat_tbl).filter(
t_marketing_time_stat_tbl.c.mailing_id==mid)
try:
return res.one() + res_t.one()
except NoResultFound as e:
return None
res
and res_t
are both of type sqlalchemy.util._collections.KeyedTuple
which support addition, however, the resulting object is a simple tuple, without keys.
I would like to have a combined result, so I did the following:
rv = {}
res=session.query(t_marketing_mailing_stats_tbl).filter(
t_marketing_mailing_stats_tbl.c.mailing_id==mid)
res_t=session.query(t_marketing_time_stat_tbl).filter(
t_marketing_time_stat_tbl.c.mailing_id==mid)
try:
res =res.one()
res_t = res_t.one()
for k in res.keys():
rv[k] = getattr(res, k)
for k in res_t.keys():
rv[k] = getattr(res_t, k)
except NoResultFound as e:
return None
While this works, I suspect there is a built-in way, or a better way of achieving this with SQLAlchemy.