我想在 SQLalchemy 中运行以下 postgreSQL 查询:
select c.*
from comments c
join (
values
(1,1),
(3,2),
(2,3),
(4,4)
) as x (id, ordering) on c.id = x.id
order by x.ordering
是否可以加入列表列表或元组列表之类的东西,并使用它们在 SQLalchemy 中提供排序?
最佳答案
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),
prefixes=['TEMPORARY']
)
temp_table.create(bind=dbsession.bind, checkfirst=True)
dbsession.execute(temp_table.insert().values(VALUES))
# Now you can query it
dbsession.query(Comments)\
.join(temp_table, Comments.id == temp_table.c.id)\
.order_by(temp_table.c.ordering)\
.all()
关于python - 在 sqlalchemy 中加入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036221/