jackc / pgx

PostgreSQL driver and toolkit for Go
MIT License
10.75k stars 838 forks source link

Can not catch the error when the input value is invalid for an enum type. #1245

Closed ns-stang closed 2 years ago

ns-stang commented 2 years ago

HI, in my code, I found that the pgx pool cannot catch the error when the input value is invalid for an enum type. Is this a bug or I am doing something wrong?

My DDL:

CREATE TYPE orch.test_status AS ENUM ('granted', 'not_granted');

CREATE TABLE IF NOT EXISTS orch.test_status_table (
        permission_status orch.test_status default 'not_granted'
)

My test code

package main

import (
    "context"
    "database/sql"
    "fmt"
    "github.com/jackc/pgx/v4/pgxpool"
    _ "github.com/lib/pq"
    "os"
)

const (
    host     = "localhost"
    port     = 5432
    user     = "postgres"
    password = "my_password"
    dbname   = "cspm"
)

func main() {
    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",
        host, port, user, password, dbname)
    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }
    defer db.Close()
    err = db.Ping()
    if err != nil {
        panic(err)
    }
    const sql = `UPDATE orch.test_status_table SET permission_status = $1`
    _, err = db.Query(sql, "test")
    if err != nil {
        fmt.Println("other driver" + err.Error())
    }
    dbPool, err := pgxpool.Connect(context.Background(), psqlInfo)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
        os.Exit(1)
    }
    err = dbPool.Ping(context.Background())
    if err != nil {
        panic(err)
    }
    _, err = dbPool.Query(context.Background(), sql, "test")
    if err != nil {
        fmt.Println("pgx driver" + err.Error())
    }
}
jackc commented 2 years ago

Err() on the returned Rows must be checked after the Rows is closed to determine if the query executed successfully

See https://pkg.go.dev/github.com/jackc/pgx/v4#Conn.Query.