Scanning into a UUID #1918
Answered
by
jackc
DanielvPhung
asked this question in
Q&A
Scanning into a UUID
#1918
-
Hello, having issues with the pgtype.UUID. Not sure if I am doing this correctly.
in postgres, my id is defined as: I get this error:
Really stumped here. Am i missing anything? Does the pgtype.uuid not supported the postgres type listed? Not sure how to deal with this. Did some research, but things seem a little outdated when looking at previous resources. |
Beta Was this translation helpful? Give feedback.
Answered by
jackc
Feb 26, 2024
Replies: 1 comment 3 replies
-
Works for me: package main
import (
"context"
"log"
"os"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
)
func main() {
ctx := context.Background()
conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
defer conn.Close(ctx)
var userID pgtype.UUID
err = conn.QueryRow(ctx, "select gen_random_uuid()").Scan(&userID)
if err != nil {
log.Fatal(err)
}
log.Println(userID.Value())
}
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As far as pgx is concerned, it doesn't care what the query actually is, just what arguments it takes and what results it returns. i.e. there shouldn't be any different in the queries. I'm fairly certain the problem is somewhere else in your application code.
See if you can duplicate the issue in a completely standalone example. Also, see if the insert works in psql. I would only expect to see that PostgreSQL error with an invalid argument ... for example, if
hashPassword
is a[]byte
with raw binary values and you are inserting it into a PostgreSQLtext
column.