drootnar drootnar - 1 year ago 118
Python Question

join values in sqlalchemy

I would like to run following postgreSQL query in SQLalchemy:

select c.*
from comments c
join (
) as x (id, ordering) on c.id = x.id
order by x.ordering

Is it possible to join something like list of lists or list of tuples and use them to provide ordering in SQLalchemy?

Answer Source
from sqlalchemy import *

from yourdbmodule import dbsession

VALUES = ((1, 1), (3, 2), (2, 3), (4, 4))

temp_table = Table(
    "temp_table", MetaData(),
    Column("id", INT, primary_key=True),
    Column("ordering", INT),


# Now you can query it
    .join(temp_table, Comments.id == temp_table.c.id)\
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download