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

V1.23.x AutoMigrate Bug(first ok, but when run more than once, will fire error). #5175

Closed
piyongcai opened this issue Mar 19, 2022 · 11 comments
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@piyongcai
Copy link
Contributor

piyongcai commented Mar 19, 2022

GORM Playground Link

go-gorm/playground#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...")
}
@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Mar 19, 2022
@github-actions
Copy link

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.ioSearch Before Asking

@piyongcai piyongcai changed the title V1.23.x AutoMigrate Bug. V1.23.x AutoMigrate Bug(fist ok, but when run more than once, will be panic). Mar 19, 2022
@piyongcai piyongcai changed the title V1.23.x AutoMigrate Bug(fist ok, but when run more than once, will be panic). V1.23.x AutoMigrate Bug(fist ok, but when run more than once, will fire error). Mar 19, 2022
@github-actions github-actions bot added type:with reproduction steps with reproduction steps and removed type:missing reproduction steps missing reproduction steps labels Mar 19, 2022
@piyongcai piyongcai changed the title V1.23.x AutoMigrate Bug(fist ok, but when run more than once, will fire error). V1.23.x AutoMigrate Bug(first ok, but when run more than once, will fire error). Mar 19, 2022
@muety
Copy link

muety commented Mar 26, 2022

I'm having a similar issue with SQLite, see muety/wakapi#346.

@LouisSayers
Copy link

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

@exfly
Copy link

exfly commented Mar 29, 2022

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

@a631807682
Copy link
Member

go-gorm/mysql#64

@creack
Copy link

creack commented Apr 2, 2022

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.

@muety
Copy link

muety commented Apr 2, 2022

Had the same behavior as @creack with Postgres.

@byroncoetsee
Copy link

Same issue this side with Postgres. Reverting back down to 1.22 fixes things.

@jinzhu
Copy link
Member

jinzhu commented Apr 3, 2022

Sorry for the issue, please upgrade mysql/postgres/sqlserver driver to support it.

@CezaryTarnowski-TomTom
Copy link

I think this is still a case in the latest version for Postgres and third auto migration (the second works fine):
go-gorm/playground#462

@medmin
Copy link

medmin commented Aug 8, 2022

Is there a way to turn off AutoMigrate ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

10 participants