如何使用传递给函数的查询实现flask sqlalchemy查询

时间:2014-07-01 15:24:50

标签: sqlalchemy mysql-python flask-sqlalchemy

我试图将这个mysql查询转换为flask sqlalchemy:

    SELECT * FROM (*results query*) AS someDocuments WHERE agency in (SELECT agency FROM document WHERE agency ='Comptroller');

我试图在函数refine_search中执行此操作,其中用户将结果查询作为参数发送,其中包含代理等字段。我试过了

    refined = db.session.query(results).filter(results.agency.in_(db.session.query(Document.agency).filter(Document.agency == 'Comptroller')))

但我收到了错误

    InvalidRequestError: SQL expression, column, or mapped entity expected - got '<flask_sqlalchemy.BaseQuery object at 0x0331B930>'

我试过了

    refined = db.session.query(Document).filter(Document.agency.in_(db.session.query(results).filter(results.agency == 'Comptroller')))

但是我得到了类似的错误

InvalidRequestError: SQL expression, column, or mapped entity expected - got '<flask_sqlalchemy.BaseQuery object at 0x0267B2D0>'

我也试过

    refined = db.session.query(Document).filter(results.agency.in_(db.session.query(Document.agency).filter(Document.agency == 'Comptroller')))

但我收到了错误

   AttributeError: 'BaseQuery' object has no attribute 'agency'

这样做。

我对如何使用flask sqlalchemy语法

创建此查询感到非常困惑

0 个答案:

没有答案