简体   繁体   English

为什么我的flask-sqlalchemy查询失败

[英]Why is my this flask-sqlalchemy query failing

I am trying to write a query which emits the stock data for a given ticker for the current day. 我正在尝试编写一个查询,该查询发出当天特定股票行情的股票数据。 My underlying model records the date and time in a datetime field. 我的基础模型在datetime字段中记录日期和时间。 Currently, I am trying to query by ticker and then time_stamp. 目前,我正在尝试按代码查询,然后查询time_stamp。

@socketio.on("get initial data")
def on_initial_data(ticker):
    """
    Emits the initial stock data for given ticker.
    :param str ticker: ticker of stock to query.
    """

    socketio.emit("initial data",
                  [i.serialize for i
                   in Stock.query.filter_by(
                      ticker=ticker,
                      time_stamp=db.func.date(Stock.time_stamp) == date.today()).order_by(Stock.time_stamp)],
                  room=request.sid)

When this is executed however, I get the error: 但是,当执行此命令时,出现错误:

HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

I am not sure why I am getting this because isn't the db.func.date() casting? 我不确定为什么要得到这个,因为不是db.func.date()强制转换吗?

You've mixed filter_by() and filter() . 您已经混合了filter_by()filter() See What's the difference between filter and filter_by in SQLAlchemy? 请参阅SQLAlchemy中的filter和filter_by有什么区别? and Flask-SQLAlchemy - Greater than or equal to . Flask-SQLAlchemy-大于或等于 In short, you're comparing time_stamp for equality with an expression producing a boolean and the resulting query might look something like: 简而言之,您正在将time_stamp的相等性与产生布尔值的表达式进行比较,结果查询可能类似于:

... WHERE ticker = :ticker AND time_stamp = (DATE(time_stamp) = :date) ...

Instead: 代替:

Stock.query.\
    filter_by(ticker=ticker).\
    filter(db.func.date(Stock.time_stamp) == date.today()).\
    order_by(Stock.time_stamp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM