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

db.AutoMigrate failed on postgres after adding new field to struct #5702

Closed
axiangcoding opened this issue Sep 17, 2022 · 1 comment · Fixed by #5705
Closed

db.AutoMigrate failed on postgres after adding new field to struct #5702

axiangcoding opened this issue Sep 17, 2022 · 1 comment · Fixed by #5705
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@axiangcoding
Copy link

axiangcoding commented Sep 17, 2022

GORM Playground Link

go-gorm/playground#515

Description

I am using AutoMigrate as a method of database updates. But when I added some fields to the model, this function reported an error

database: postgres:14.4
gorm version v1.23.8

struct GameUser was

type GameUser struct {
	gorm.Model
	XXX1 string `gorm:"uniqueIndex;size:255"`
	XXX2 string `gorm:"size:255"`
	
	StatAb UserStat `gorm:"embedded;embeddedPrefix:stat_ab_"`
	StatRb UserStat `gorm:"embedded;embeddedPrefix:stat_rb_"`
	StatSb UserStat `gorm:"embedded;embeddedPrefix:stat_sb_"`
}

type UserStat struct {
	TotalMission         int
	WinRate              float64
	GroundDestroyCount   int
	FleetDestroyCount    int
	GameTime             string
	AviationDestroyCount int
	WinCount             int
	SliverEagleEarned    int64
	DeadCount            int
}

And i did some modification to struct GameUser

type GameUser struct {
	gorm.Model
	XXX1 string `gorm:"uniqueIndex;size:255"`
	XXX2 string `gorm:"size:255"`
	
	StatAb UserStat `gorm:"embedded;embeddedPrefix:stat_ab_"`
	StatRb UserStat `gorm:"embedded;embeddedPrefix:stat_rb_"`
	StatSb UserStat `gorm:"embedded;embeddedPrefix:stat_sb_"`

        // new fields
        GroundRateAb GroundRate `gorm:"embedded;embeddedPrefix:rate_ground_ab_"`
	GroundRateRb GroundRate `gorm:"embedded;embeddedPrefix:rate_ground_rb_"`
	GroundRateSb GroundRate `gorm:"embedded;embeddedPrefix:rate_ground_sb_"`
}

type UserStat struct {
	TotalMission         int
	WinRate              float64
	GroundDestroyCount   int
	FleetDestroyCount    int
	GameTime             string
	AviationDestroyCount int
	WinCount             int
	SliverEagleEarned    int64
	DeadCount            int
}

type GroundRate struct {
	GameCount              int
	GroundVehicleGameCount int
	TDGameCount            int
	HTGameCount            int
	SPAAGameCount          int
	GameTime               string
	GroundVehicleGameTime  string
	TDGameTime             string
	HTGameTime             string
	SPAAGameTime           string
	TotalDestroyCount      int
	AviationDestroyCount   int
	GroundDestroyCount     int
	FleetDestroyCount      int
}

If game_user not in the database, it works well, new table game_user will be created. But when the table game_user which created by old struct exists, AutoMigrate error occurs. Error is

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x855f1b]

goroutine 1 [running]:
gorm.io/driver/postgres.Migrator.AlterColumn.func1(0xc0012a16c0)
        C:/Users/Administrator/go/pkg/mod/gorm.io/driver/postgres@v1.3.8/migrator.go:272 +0x23b
gorm.io/gorm/migrator.Migrator.RunWithValue({{0x60?, 0xc0012c4150?, {0x12a00e0?, 0xc00071ecc0?}}}, {0xf34580?, 0xc0000f4800}, 0xc000117618)        
        C:/Users/Administrator/go/pkg/mod/gorm.io/gorm@v1.23.8/migrator/migrator.go:52 +0x13c
gorm.io/driver/postgres.Migrator.AlterColumn({{{0x0?, 0xc0012c4150?, {0x12a00e0?, 0xc00071ecc0?}}}}, {0xf34580?, 0xc0000f4800?}, {0xe7d4dd?, 0x0?})
        C:/Users/Administrator/go/pkg/mod/gorm.io/driver/postgres@v1.3.8/migrator.go:259 +0xb1
gorm.io/gorm/migrator.Migrator.MigrateColumn({{0x4b?, 0xc001277f50?, {0x12a00e0?, 0xc00071ecc0?}}}, {0xf34580, 0xc0000f4800}, 0xc0002503c0, {0x12a2640, 0xc00036a340})
        C:/Users/Administrator/go/pkg/mod/gorm.io/gorm@v1.23.8/migrator/migrator.go:481 +0x625
gorm.io/driver/postgres.Migrator.MigrateColumn({{{0x1?, 0xc001277f50?, {0x12a00e0?, 0xc00071ecc0?}}}}, {0xf34580, 0xc0000f4800}, 0xc0002503c0, {0x12a2640?, 0xc00036a340?})
        C:/Users/Administrator/go/pkg/mod/gorm.io/driver/postgres@v1.3.8/migrator.go:227 +0x95
gorm.io/gorm/migrator.Migrator.AutoMigrate.func1(0xc00031b500)
        C:/Users/Administrator/go/pkg/mod/gorm.io/gorm@v1.23.8/migrator/migrator.go:123 +0x193
gorm.io/gorm/migrator.Migrator.RunWithValue({{0x70?, 0xc00077d770?, {0x12a00e0?, 0xc00071ecc0?}}}, {0xf34580?, 0xc0000f4800}, 0xc000117c60)
        C:/Users/Administrator/go/pkg/mod/gorm.io/gorm@v1.23.8/migrator/migrator.go:52 +0x13c
gorm.io/gorm/migrator.Migrator.AutoMigrate({{0x0?, 0xc00077d770?, {0x12a00e0?, 0xc00071ecc0?}}}, {0xc000524280?, 0x0?, 0x0?})
        C:/Users/Administrator/go/pkg/mod/gorm.io/gorm@v1.23.8/migrator/migrator.go:101 +0x1ce
gorm.io/gorm.(*DB).AutoMigrate(0xc0004fe750?, {0xc000524280, 0x2, 0x2})
        C:/Users/Administrator/go/pkg/mod/gorm.io/gorm@v1.23.8/migrator.go:28 +0x43
github.com/axiangcoding/ax-web/data.autoMigrate(0xc000117e20?)

it appears something wrong here. Anyone facing the same issue?

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Sep 17, 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

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

Successfully merging a pull request may close this issue.

3 participants