GORM Playground Link
https://github.com/go-gorm/playground/pull/451
Description
in the gorm version 1.23.x, first AutoMigrate can run ok, but when you call AutoMigrate more than once, it's will panic.
package main
import (
"fmt"
"log"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormLog "gorm.io/gorm/logger"
)
func main() {
i := 0
fmt.Println(i)
db, err := gorm.Open(postgres.New(postgres.Config{
DSN: `your dsn`,
PreferSimpleProtocol: false,
}), &gorm.Config{
Logger: gormLog.Default.LogMode(gormLog.Info),
SkipDefaultTransaction: true,
})
if err != nil {
log.Fatal(err)
}
type OrmModel struct {
ID int64
}
if err = db.Transaction(func(tx *gorm.DB) error {
return tx.AutoMigrate(&OrmModel{})
}); err != nil {
panic(err)
}
// in version "gorm.io/gorm v1.22.5, it's worked. but in version v.23.x, the flow will panic.
if err = db.Transaction(func(tx *gorm.DB) error {
return tx.AutoMigrate(&OrmModel{})
}); err != nil {
panic(err)
}
fmt.Println("...FINISH...")
}
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: muety
I'm having a similar issue with SQLite, see https://github.com/muety/wakapi/issues/346.
Comment From: LouisSayers
I'm also getting errors with AutoMigrate:
/go/pkg/mod/gorm.io/driver/postgres@v1.3.1/migrator.go:271 ERROR: relation "idx_signup_requests_hash_code" already exists
Comment From: exfly
For PG,it looks like the reason is [1.16.0/pkg/mod/gorm.io/driver/postgres]()@v1.3.1[/migrator.go:331]() driver: bad connection
sql SELECT * FROM "orm_models" LIMIT 1
Comment From: a631807682
https://github.com/go-gorm/mysql/pull/64
Comment From: creack
I am having a similar issue, I think related, but with a simpler case.
Trying to use AutoMigrate on postgres with any prefix works the first time, but not the 2nd time. Same issue when defining TableName() or when using NamePrefix config.
type Person struct {
ID int
Name string
}
func (Person) TableName() string { return "foo.Person" }
func main() {
const dsn = "host=db user=usr password=pass dbname=db port=5432 sslmode=disable"
db, _ := gorm.Open(postgres.Open(dsn), &gorm.Config{
// NamingStrategy: schema.NamingStrategy{
// TablePrefix: "foo.",
// },
})
db.Exec(`CREATE SCHEMA IF NOT EXISTS "foo";`)
if err := db.AutoMigrate(&Person{}); err != nil {
panic(err)
}
}
The first run works well, the table gets created in the proper schema with all the right fields, but the runs after that fail with:
go-mod/gorm.io/driver/postgres@v1.3.1/migrator.go:164 ERROR: column "id" of relation "Person" already exists (SQLSTATE 42701)
The same code without the schema prefix or with public. works fine each time.
Comment From: muety
Had the same behavior as @creack with Postgres.
Comment From: byroncoetsee
Same issue this side with Postgres. Reverting back down to 1.22 fixes things.
Comment From: jinzhu
Sorry for the issue, please upgrade mysql/postgres/sqlserver driver to support it.
Comment From: CezaryTarnowski-TomTom
I think this is still a case in the latest version for Postgres and third auto migration (the second works fine): https://github.com/go-gorm/playground/pull/462
Comment From: medmin
Is there a way to turn off AutoMigrate ?