I started getting a migrator error after upgrading the gorm.io/gorm v1.23.4 to gorm.io/gorm v1.23.5

Note: The issue happens when I use sql.Open with Gorm on Second Run. So first migration works fine but when I rerun the same migration it throws errorl

2022/06/09 17:20:05 /Users/khanakia/D1/www/projects/gamerapp/gamerapp_go/vendor/gorm.io/driver/postgres/migrator.go:484 pq: got 1 parameters but the statement requires 0 Screenshot 2022-06-09 at 5 23 12 PM

package main

import (
    "database/sql"
    "fmt"

    "gorm.io/driver/postgres"
    "gorm.io/gorm"
    "gorm.io/gorm/logger"
)

type Student struct {
    ID     string `json:"id" gorm:"type:varchar(36);primaryKey;"`
    UserID string `json:"userId" gorm:"type:varchar(36)"`
}

func main() {

    dbDSN := "user=postgres password=root host=localhost dbname=gorm port=5432 sslmode=disable TimeZone=Asia/Shanghai"

    dbSql, err := sql.Open("postgres", dbDSN)
    if err != nil {
        panic(err)
    }

    db, err := gorm.Open(postgres.New(postgres.Config{
        Conn: dbSql,
    }), &gorm.Config{
        Logger: logger.Default.LogMode(logger.Info),
    })


    fmt.Println(err)

    db.AutoMigrate(
        &Student{},
    )
}

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: khanakia

Playground Link : https://github.com/khanakia/gorm_automigrate_error

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: gstvds

@khanakia I was having the same issue using Gorm v1.23.5 and Postgres Driver v1.3.7. I managed to solve this by downgrading Postgres to v1.3.5.

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: khanakia

@gstvds Thanks for the solution it worked for now.

@jinzhu We need to fix this bug. Thanks in advance 👍

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: a631807682

https://github.com/go-gorm/postgres is a driver for https://github.com/jackc/pgx , not for https://github.com/lib/pq . You should use like this

db, err := gorm.Open(postgres.New(postgres.Config{
  DSN: "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai", // data source name, refer https://github.com/jackc/pgx
  PreferSimpleProtocol: true, // disables implicit prepared statement usage. By default pgx automatically uses the extended protocol
}), &gorm.Config{})

Or use stdlib to open sql.DB

config, err := pgx.ParseConfig(dbDSN)
if err != nil {
  return
}

dbSql := stdlib.OpenDB(*config)

db, err := gorm.Open(postgres.New(postgres.Config{
  Conn: dbSql,
}), &gorm.Config{})

Comment From: franciscomerdot

Still have issues with this solution.

We need to use the driver cloudsqlpostgres from github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres cause the database is hosted in Google Cloud and we access it with CouldSql-Proxy.

PreferSimpleProtocol: true didn't work. Had to downgrade to v1.3.5

Comment From: a631807682

You have two ways to support it 1. Create a gorm driver to support lib/pq 2. Support jackc/pgx in GoogleCloudPlatform

As https://github.com/lib/pq#status says, we don't plan to maintain a driver for it, but you are welcome to create and maintain it if you want

Comment From: shanjunmei

I think the root of the problem is that gorm is no longer compatible with lib/pq,i try switch to pgx ,it's work now

Comment From: shanjunmei

I had the same problem when I switched from lib/pq to pgx to upgrade gorm 22.3 to 25.2, anyone know what to do about it?

type GeoPoint struct { Lng float64json:"lng"Lat float64json:"lat"Alt float64json:"alt"}

` // Value converts the given Geometry4326 struct into EWKB such that it can be stored in a // database. Implements Valuer interface for use with database operations. func (g GeoPoint) Value() (driver.Value, error) { var x = geom.NewPoint(geom.XYZ).MustSetCoords(geom.Coord{g.Lng, g.Lat, g.Alt}).SetSRID(4326) data, err := ewkb.Marshal(x, binary.LittleEndian) if err != nil { return nil, err } return data, nil }

// Scan converts the hexadecimal representation of geometry into the given Geometry4326 // struct. Implements Scanner interface for use with database operations. func (g GeoPoint) Scan(value interface{}) error { data, err := hex.DecodeString(string(value.([]byte))) if err != nil { return err } var px, err1 = ewkb.Unmarshal(data) if err1 != nil { return err1 } var geomt = px.(geom.T) var geomp = geomt.(geom.Point) g.Lng = geomp.Coords()[0] g.Lat = geomp.Coords()[1] g.Alt = geomp.Coords()[2] return nil } "github.com/twpayne/go-geom" "github.com/twpayne/go-geom/encoding/ewkb"`

The code snippet involved in the above