【问题标题】:Postgres OVERLAPS date function in TypeORMTypeORM中的Postgres OVERLAPS日期函数
【发布时间】:2022-05-10 17:13:28
【问题描述】:

我想将查询转移到 TypeORM。

在 Postgres 中看起来像这样

SELECT * 
from equipment_charging 
where (equipment_charging."start_date"::date, equipment_charging."end_date"::date) OVERLAPS (DATE '2020-09-10', DATE '2020-09-14')

我在 TypeORM 中这样写这个查询:

.where(
  '(equipment_charging.startDate::date, equipment_charging.endDate::date) OVERLAPS (DATE :startDate, DATE :endDate)',
  { startDate, endDate },
)

它给了我这样的错误:

{"context":"ExceptionsHandler","stack":["QueryFailedError: syntax error at or near \"$1\"\n    at new QueryFailedError 

我做错了什么?

【问题讨论】:

  • 我不知道TypeORM但是尝试去掉参数前面的DATE关键字。它仅对键入的常量值需要,但如果 startDateendDate 已经是 DATE 实例,则不需要它们
  • 非常感谢,不幸的是它没有工作......它说“\”或附近的语法错误,\“”不知道这是哪个逗号:(

标签: postgresql nestjs typeorm


【解决方案1】:

如果 equipment_charging.startDate 和 equipment_charging.endDate 已经是数据库中的日期类型,我认为您可以删除 ::date

同样的想法,尝试直接为输入提供日期类型。

我还在 typeorm 中为查询构建器使用了很多双引号 试试这个:

.where(
  '("equipment_charging"."startDate", "equipment_charging"."endDate") OVERLAPS (:startDate, :endDate)',
  { startDate, endDate },
)

如果需要,将 startDate 和 endDate 转换为日期(例如使用日期 fns 的 parseIso)

【讨论】:

    猜你喜欢
    • 2014-12-01
    • 2010-11-13
    • 2017-12-11
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多