Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot scan/insert user defined UUIDs. #2107

Closed
dfonnegra opened this issue Aug 13, 2024 · 0 comments
Closed

Cannot scan/insert user defined UUIDs. #2107

dfonnegra opened this issue Aug 13, 2024 · 0 comments
Labels

Comments

@dfonnegra
Copy link

dfonnegra commented Aug 13, 2024

Describe the bug
After migrating from v4 to v5. Inserting or scanning custom defined uuid's stopped working.

To Reproduce
Steps to reproduce the behavior:

If possible, please provide runnable example such as:

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/gofrs/uuid/v5"
	pgxuuid "github.com/jackc/pgx-gofrs-uuid"
	"github.com/jackc/pgx/v5"
	"github.com/jackc/pgx/v5/pgxpool"
)

type ProviderID uuid.UUID

func main() {
	dbconfig, err := pgxpool.ParseConfig(os.Getenv("DATABASE_URL"))
	if err != nil {
		fmt.Println("could not parse db config: ", err)
		return
	}

	ctx := context.Background()
	dbconfig.AfterConnect = func(_ context.Context, conn *pgx.Conn) error {
		pgxuuid.Register(conn.TypeMap())
		return nil
	}

	pool, err := pgxpool.NewWithConfig(ctx, dbconfig)
	if err != nil {
		fmt.Println("could not create pool: ", err)
		return
	}
	defer pool.Close()

	_, err = pool.Exec(ctx, "CREATE TABLE IF NOT EXISTS test (id UUID PRIMARY KEY)")
	if err != nil {
		fmt.Println("could not create table: ", err)
		return
	}

	_, err = pool.Exec(ctx, "INSERT INTO test (id) VALUES ($1)", ProviderID(uuid.Must(uuid.NewV4())))
	if err != nil {
		fmt.Println("could not insert value: ", err)
		return
	}

	rows, err := pool.Query(ctx, "SELECT id FROM test")
	if err != nil {
		fmt.Println("could not query: ", err)
		return
	}
	defer rows.Close()
	for rows.Next() {
		var id ProviderID
		if err := rows.Scan(&id); err != nil {
			fmt.Println("could not scan: ", err)
			return
		}
		fmt.Println("id: ", id)
	}
}

Expected behavior
pgx should detect that the variable is an uuid and encode/decode it properly.

Actual behavior
It fails. When scanning this is the error:

could not scan:  can't scan into dest[0]: cannot scan uuid (OID 2950) in binary format into *main.ProviderID

when inserting this is the error:

could not insert value:  failed to encode args[0]: unable to encode main.ProviderID{0x84, 0x33, 0x6c, 0x48, 0x7, 0x10, 0x4a, 0xe2, 0x88, 0xc5, 0x49, 0xbb, 0xef, 0x7a, 0x3f, 0x30} into binary format for uuid (OID 2950): cannot find encode plan

However if I change the custom uuids by raw uuids, it works.

Version

  • Go: $ go version -> go version go1.21.5 darwin/arm64
  • pgx: $ grep 'github.com/jackc/pgx/v[0-9]' go.mod -> github.com/jackc/pgx/v5 v5.6.0

Additional context
This represents a huge migration burden as the app contains more than 1.000 places where this needs to be fixed and I'm sure it's not uncommon.

@dfonnegra dfonnegra added the bug label Aug 13, 2024
@dfonnegra dfonnegra changed the title Cannot scan user defined UUIDs. failed to insert test framework providers: error inserting provider: can't scan into dest[0]: cannot scan uuid (OID 2950) in binary format into *myUUID Cannot scan user defined UUIDs. Aug 13, 2024
@dfonnegra dfonnegra changed the title Cannot scan user defined UUIDs. Cannot scan/insert user defined UUIDs. Aug 13, 2024
@jackc jackc closed this as completed in 5747f37 Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant