Converting a mysql with Nested SELECT, EXIST, SELECT NEXT clauses to a python sqlalchemy statement

StackOverflow https://stackoverflow.com/questions/22361772

質問

So i have this statement

SELECT h.usd FROM historic h, block b 
WHERE UNIX_TIMESTAMP(h.timestamp) <=b.block_nTime 
AND NOT EXISTS 
(SELECT 'NEXT' FROM 
historic h2 where h2.timestamp > h.timestamp 
AND UNIX_TIMESTAMP(h2.timestamp) <=b.block_nTime) AND b.block_id = 'id'

I'm trying to convert this to a python sqlalchemy statement - this is how far i got.

from sqlalchemy import *
from sqlalchemy.sql import func
from sqlalchemy.orm import Session

#{...}

h = alias(historic)
h2 = alias(historic)

session.query(h.c.usd).filter(
and_(h.c.timestamp <= block.c.block_nTime,   
~exists(
func.next_value(Sequence(h2.c.timestamp))
).where(
and_(h2.c.timestamp > h.c.timestamp, h2.c.timestamp <= block.c.block_nTime
) & block.c.id == block_id)))

some how i get this error as well:

sqlalchemy.exc.ArgumentError: columns argument to select() must be a Python list or other iterable

Think im not using Sequence() correctly. What may the issue be?

Thanks in advance!

役に立ちましたか?

解決

The error you get references the exists. Just wrap the parameter in a list and it should work:

Before: ~exists(func.next_value(Sequence(h2.c.timestamp)))
After: ~exists([func.next_value(Sequence(h2.c.timestamp))])

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top