How should I open a gorm connection alongside a dockertest pool, while keeping TranslateError: true?

I'm trying to implement some tests for the DB layer of my backend. For this I'm using the dockertest package, creating an ephemeral docker container with a postgresDB just for the tests. This is how I'm opening that connection inside TestMain() at the moment:

        pool, err := dockertest.NewPool("")
    if err != nil {
        log.Fatalf("Could not construct pool: %s", err)
    }

    err = pool.Client.Ping()
    if err != nil {
        log.Fatalf("Could not connect to docker: %s", err)
    }

    resource, err := pool.RunWithOptions(&dockertest.RunOptions{
        Repository: "postgres",
        Tag:        "11",
        Env: []string{
            "POSTGRES_PASSWORD=testpgpassword",
            "POSTGRES_USER=testpguser",
            "POSTGRES_DB=testpgdb",
            "listen_addresses= '*'",
        },
    },
        func(config *docker.HostConfig) {
            config.AutoRemove = true
            config.RestartPolicy = docker.RestartPolicy{Name: "no"}
        })

    if err != nil {
        log.Fatalf("Could not start resource: %s", err)
    }

    hostPort := resource.GetHostPort("5432/tcp")
    databaseUrl := fmt.Sprintf("postgres://testpguser:testpgpassword@%s/testpgdb?sslmode=disable", hostPort)

    // exponential backoff-retry, because the application in the container might not be ready to accept connections yet
    pool.MaxWait = 120 * time.Second
    var sqlDB *sql.DB
    if err = pool.Retry(func() error {
        sqlDB, err = sql.Open("postgres", databaseUrl)
        if err != nil {
            return err
        }

        return sqlDB.Ping()
    }); err != nil {
        log.Fatalf("Could not connect to docker: %s", err)
    }

    gormDB, err := gorm.Open(postgres.New(postgres.Config{Conn: sqlDB}), &gorm.Config{TranslateError: true})
    if err != nil {
        log.Fatalf("Could not create gorm DB from dockertest sql connection: %s", err)
    }

However, this method seems to ignore the TranslateError:true option for some reason. My conditional statements that check for gorm.ErrDuplicatedKey are never entered during tests, despite working just fine when using go run main.go. However, main.go does use a different method for opening the *gorm.DB connection:

    connStr := fmt.Sprintf("host=%v port=%v user=%v password=%v dbname=%v sslmode=%v", dbHost, dbPort, dbUser, dbPass, dbName, dbSSL)
    db, err := gorm.Open(postgres.Open(connStr), &gorm.Config{TranslateError: true})

I don't think this is a bug, I must be doing something wrong, but the errors are indeed not being translated when opening the connection as shown in the first snippet of code.

The document you expected this should be explained

https://gorm.io/docs/error_handling.html

Expected answer

How should I properly use gorm.Open in the first snippet of Code so that my tests pass (they expect the error translation to work properly)

Comment From: a631807682

postgres driver is special, due to historical reasons, we support pg and pgx at the same time, can TranslateError handle errors separately according to DriverName? @saeidee

Comment From: saeidee

@guillermo-st as @a631807682 mentioned it is because under the hood, you are using two different drivers in your test and main file, and currently we are checking the error translation based on pgx error types.

I will prepare a PR to enable error translation for pg driver as well.

Comment From: guillermo-st

@saeidee does that mean that if I open the sql connection with pgx in my dockertest setup, the TranslateError flag will work?

Thanks a lot for your reply.

Comment From: saeidee

@guillermo-st you may try to use https://github.com/jackc/pgx instead of https://github.com/lib/pq. Here is an example https://github.com/jackc/pgx/wiki/Getting-started-with-pgx-through-database-sql.

Comment From: saeidee

Since there is a fix PR I am closing this issue.