Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to custom the interval data type? #2580

Answered by andrewmbenton
katatrina asked this question in Q&A
Discussion options

I have this query:

-- name: CreateSnippet :one
INSERT INTO snippets (title, content, created_at, expires)
VALUES(1,ドル 2,ドル CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + INTERVAL '7 days')
RETURNING *;

I want the method CreateSnippet also taking a day number (like 7) as an argument or even the whole interval as an argument. What should I do?

You must be logged in to vote

If you use pgx/v5 as your driver then it looks like sqlc will give you a pgtype.Interval to use as a parameter, which is probably pretty comfortable to use. https://play.sqlc.dev/p/0ae27106633b32760f9c1072009605e1b294a1ed7563290509c7daa462796a31

If you're not using pgx/v5 then I think the above suggestion from @orisano to use MAKE_INTERVAL is best: https://play.sqlc.dev/p/147a2e77fe7e64ffd21af152bb67e447b616bf388410a225df25bde7ed1f0f5e

Replies: 2 comments 2 replies

Comment options

Please use MAKE_INTERVAL or (3ドル * INTERVAL '1 day')
https://stackoverflow.com/a/74481697

You must be logged in to vote
0 replies
Comment options

If you use pgx/v5 as your driver then it looks like sqlc will give you a pgtype.Interval to use as a parameter, which is probably pretty comfortable to use. https://play.sqlc.dev/p/0ae27106633b32760f9c1072009605e1b294a1ed7563290509c7daa462796a31

If you're not using pgx/v5 then I think the above suggestion from @orisano to use MAKE_INTERVAL is best: https://play.sqlc.dev/p/147a2e77fe7e64ffd21af152bb67e447b616bf388410a225df25bde7ed1f0f5e

You must be logged in to vote
2 replies
Comment options

hi, I tried the approach with a SELECT query like the following:

SELECT *
FROM table
WHERE create_date >= NOW() - INTERVAL $1

And is failing with sqlc 1.26.0 with the following error:
ERROR: syntax error at or near \"1ドル\" (SQLSTATE 42601).

I'm using the generated code:

queries.TestQuery(ctx, pgtype.Interval{Days: 7, Valid: true})

Do you know what I'm missing?

Comment options

I had the same issue as @soasada and solved it by type casting the parameter like:

SELECT *
FROM table
WHERE create_date >= NOW() - $1::INTERVAL

Versions:

  • pgx v5.7.4
  • sqlc v1.29.0
  • go 1.24.2
  • postgresql 17.5
Answer selected by andrewmbenton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /