引用参数到golang sqlc

szqfcxe2  于 2023-06-19  发布在  Go
关注(0)|答案(1)|浏览(143)

使用sqlc生成db方法。有以下查询

-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL '30' SECOND);

我想传递30作为参数,但我使用了下面的查询,然后引号不来,数据库给出错误。

-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL $1 SECOND);

尝试引用$1,但sqlc认为它是字符串

-- name: RemoveRows:exec
DELETE FROM demotable
WHERE last_updated < (CURRENT_TIMESTAMP - INTERVAL '$1' SECOND);
s4n0splo

s4n0splo1#

或者,与其在尝试欺骗类型系统时瞎忙,不如直接做

... - ($1 * interval '1 minute')

相关问题