Movatterモバイル変換


[0]ホーム

URL:


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

Answeredbyandrewmbenton
katatrina asked this question inQ&A
Discussion options

I have this query:

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

I want the methodCreateSnippet 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 usepgx/v5 as your driver then it looks like sqlc will give you apgtype.Interval to use as a parameter, which is probably pretty comfortable to use.https://play.sqlc.dev/p/0ae27106633b32760f9c1072009605e1b294a1ed7563290509c7daa462796a31

If you're not usingpgx/v5 then I think the above suggestion from@orisano to useMAKE_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 usepgx/v5 as your driver then it looks like sqlc will give you apgtype.Interval to use as a parameter, which is probably pretty comfortable to use.https://play.sqlc.dev/p/0ae27106633b32760f9c1072009605e1b294a1ed7563290509c7daa462796a31

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

You must be logged in to vote
2 replies
@soasada
Comment options

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

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

And is failing with sqlc1.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?

@brahmlower
Comment options

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

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

Versions:

  • pgx v5.7.4
  • sqlc v1.29.0
  • go 1.24.2
  • postgresql 17.5
Answer selected byandrewmbenton
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
5 participants
@katatrina@andrewmbenton@orisano@brahmlower@soasada

[8]ページ先頭

©2009-2025 Movatter.jp