【问题标题】:PostgreSQL Hasura Computed field operator does not exist: bigint = textPostgreSQL Hasura 计算字段运算符不存在:bigint = text
【发布时间】:2021-06-22 15:00:09
【问题描述】:

我正在尝试为 Hasura (PostgreSQL) 中的计算域创建保留函数。我正在尝试制作的代码从 rooms 表中获取 game_length 并返回带有 game_length + start_time 的时间戳。但是,我是 SQL 函数的新手,我似乎无法让这个函数工作,因为我收到一个错误,指出:postgres-error:运算符不存在:bigint = text。有谁知道我做错了什么?

测试表组成:id(int)、start_time(timestamp)、game_length(int)、room_id(int)

CREATE OR REPLACE FUNCTION public.calculate_reservation_total_time(t test)
    RETURNS timestamp with time zone
    LANGUAGE sql
    STABLE
AS $function$  
    SELECT t.start_time + (SELECT count(*) FROM rooms WHERE id=t.room_id ||' minutes')::interval; 
$function$

【问题讨论】:

  • 那不是FROM t,或者如果你使用rooms,那么rooms.room_id。不太确定t rooms 到底是干什么用的?
  • 请添加您的类型的定义test。我的猜测是 rooms.idbigint 类型,而 t.room_idtext 类型。因此,您需要在比较它们之前将它们转换为另一种。
  • @Islingre 明白了!好像是这样的,有两个整数

标签: postgresql function hasura


【解决方案1】:

您的括号中有错误:

(SELECT count(*) FROM rooms WHERE id=t.room_id ||' minutes')::interval

这会将t.room_id' minutes' 连接起来(因此您会得到text 类型的内容),然后将其与rooms 中的id 进行比较。

试试下面的语句:

CREATE OR REPLACE FUNCTION public.calculate_reservation_total_time(t test)
    RETURNS timestamp with time zone
    LANGUAGE sql
    STABLE
AS $function$  
    SELECT t.start_time +
        ((SELECT count(*) FROM rooms WHERE id=t.room_id) ||' minutes')::interval; 
$function$

【讨论】:

    猜你喜欢
    • 2018-05-05
    • 2012-07-02
    • 2019-07-03
    • 2020-08-27
    • 2021-12-09
    • 2021-11-12
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多