【问题标题】:SQLAlchemy complex querySQLAlchemy 复杂查询
【发布时间】:2023-04-08 06:20:03
【问题描述】:

今天我遇到了一个 sqlalchemy 查询的问题。我已经尝试了一段时间,但我无法弄清楚我做错了什么。 我对 sqlalchemy 的整个故事很陌生,我已经开始习惯于中等简单的查询,但我无法解决以下问题:

units = Unit.query.join(Campaign).join(ServedUnitMetric, and_(ServedUnitMetric.client_id == clientID)).filter(
            (Campaign.active_applications.any(and_(ActiveApplication.application_id == client.application_id, ActiveApplication.enabled == True)))                    &
            (Campaign.targeting_setting.has(TargetingSetting.countries.any(and_(Country.country_code == client.locale.country_code, Country.is_active == True))))     &
            (Campaign.expiration_date > datetime.utcnow())                                                                                                            &
            (Campaign.active_campaign.has(ActiveCampaign.expenses + item.price <= Campaign.budget))                                                                   &
            (Campaign.active_campaign.has(ActiveCampaign.status == CAMPAIGN_STATUS_ACTIVE))                                                                           &
            (Campaign.max_bid >= item.price)                                                                                                                          &
            (Unit.serve_setting.has(and_(ServeSetting.language_code == client.locale.language_code, ServeSetting.valid_from <= datetime.utcnow(), ServeSetting.valid_to >= datetime.utcnow()))) &

        (
            ((Unit.serve_setting.has(ServeSetting.serve_once_per_period == True)) &  (ServedUnitMetric.unit_id != Unit.id))                                         |
            ((Unit.serve_setting.has(ServeSetting.serve_once_per_period == True)) &  (ServedUnitMetric.unit_id == Unit.id) & (datetime.utcnow() > (ServedUnitMetric.endDate + ServeSetting.period_hours)))
        )
        )

查询的第一部分正在运行,现在我正在尝试使第二部分正常运行,但我仍然遇到问题。 它总是返回空数据,但我已经准备好测试并且我确信它应该返回数据。

我收到了错误,但我仍然不知道如何解决它。问题出在这里

ServedUnitMetric.endDate + ServeSetting.period_hours

SQLAlchemy 似乎没有正确翻译 DateTime + Interval。看起来这句话被翻译成双精度值。现在是关于了解如何将 DateTime 添加到 Interval 并且应该完成的技巧。 我正在尝试使用 func.adddate 但它会溢出,我想是由于 Interval 是 1970-1-1 + 您输入的间隔。

感谢https://stackoverflow.com/a/17236032/956541,我终于找到了一个可行的解决方案

所以那行变成:

((Unit.serve_setting.has(ServeSetting.serve_once_per_period == True)) & (ServedUnitMetric.unit_id == Unit.id) & (func.abs(func.unix_timestamp(datetime.utcnow()) - func.unix_timestamp(ServedUnitMetric.endDate)) >= (ServeSetting.period_hours * 60 * 60)))

感谢您的帮助,

恩里科

【问题讨论】:

  • 我建议看一下生成的 SQL,看看它是否有意义。如果没有模型定义、样本数据和预期输出,很难判断这里出了什么问题。
  • 确实,看看生成的SQL 语句。我假设您在.period_hours 中的表达在mysql 中没有正确翻译。解决方案here 可能会有所帮助。
  • 嗨,Audrius,感谢您的建议,我已启用 sqlachemy 日志记录,正在尝试找出问题所在。我会试一试,如果找不到解决方案,我会更新帖子。
  • 还要感谢 van :D 我将立即尝试您的解决方案
  • 如果您找到了解决方案,与其编辑问题,不如将其发布为答案并接受它,以便其他有类似问题的人可以看到它已解决。

标签: python mysql sql sqlalchemy flask-sqlalchemy


【解决方案1】:

解决办法是func.timestamp(date)

((Unit.serve_setting.has(ServeSetting.serve_once_per_period == True)) & (ServedUnitMetric.unit_id == Unit.id) & (func.abs(func.unix_timestamp(datetime.utcnow()) - func.unix_timestamp(ServedUnitMetric.endDate)) >= (ServeSetting.period_hours * 60 * 60)))

感谢https://stackoverflow.com/a/17236032/956541

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 2011-09-18
    相关资源
    最近更新 更多