Replies: 1 comment
-
Your are using a package main
import (
"context"
"database/sql/driver"
"log"
"os"
"github.com/jackc/pgx/v5"
)
type Category struct {
Color int `json:"color"`
}
type CategorySlice []Category
func (slice CategorySlice) Value() (driver.Value, error) {
if len(slice) == 0 {
return nil, nil
}
return []byte(`{"foo": "bar"}`), nil
}
func main() {
ctx := context.Background()
conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
defer conn.Close(ctx)
categories := CategorySlice{{Color: 1}, {Color: 2}, {Color: 3}}
var result string
err = conn.QueryRow(ctx, `select $1::json`, categories).Scan(&result)
if err != nil {
log.Fatalf("QueryRow failed: %s", err)
}
log.Println(result)
} Output:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I'm looking into converting from sql.DB to pgx, found an issue with converting between slices and JSONB columns.
and then
The
Value
function is not called when using pgx. It is when usingsql.DB
.Looks like a bug?
Beta Was this translation helpful? Give feedback.
All reactions