gorm icon indicating copy to clipboard operation
gorm copied to clipboard

Regression of Scan when loading pointer object in a struct

Open tab1293 opened this issue 2 years ago • 6 comments

GORM Playground Link

https://github.com/go-gorm/playground/pull/504

Description

There seems to have been a regression from v1.23.5 to v1.23.6 where loading objects with a pointer struct embed will result in that pointer struct being duplicated across all objects loaded. The commit that caused the regression seems to be this: https://github.com/go-gorm/gorm/commit/d01de7232b46987e239ef19a89d9ab192f453894. There are already two comments on this commit mentioning this behavior.

Can we expect a fix to change this behavior back to not duplicating embed structs across object loads?

tab1293 avatar Aug 04 '22 14:08 tab1293

related to https://github.com/go-gorm/gorm/issues/5431

a631807682 avatar Aug 04 '22 16:08 a631807682

I've also observed this issue which is blocking us from upgrading.

ianeal avatar Aug 05 '22 19:08 ianeal

@jinzhu Any update on this one? I tried with gorm v1.23.9 and the problem still exists. I can put together a sample program if that would help.

ianeal avatar Sep 20 '22 16:09 ianeal

@jinzhu any thoughts around the behavior change introduced with this update?

tab1293 avatar Sep 27 '22 16:09 tab1293

@jinzhu I agree with @a631807682 https://github.com/go-gorm/gorm/issues/5431#issuecomment-1157263863, revert #5388 solves this problem.

reproduction code

type User struct {
	*Embedded
}

type Embedded struct {
	ID   int
	Name string
}

func main() {
	db.AutoMigrate(&User{})
	db.Create([]User{
		{&Embedded{
			ID:   1,
			Name: "name1",
		}},
		{&Embedded{
			ID:   2,
			Name: "name2",
		}},
	})
	users := make([]User, 0, 2)
	db.Find(&users)
	// github.com/davecgh/go-spew/spew
	spew.Dump(users)
}

output

([]main.User) (len=2 cap=2) {
 (main.User) {
  Embedded: (*main.Embedded)(0xc0014a15f0)({
   ID: (int) 2,
   Name: (string) (len=5) "name2"
  })
 },
 (main.User) {
  Embedded: (*main.Embedded)(0xc0014a15f0)({
   ID: (int) 2,
   Name: (string) (len=5) "name2"
  })
 }
}

miraclesu avatar Oct 28 '22 08:10 miraclesu

@jinzhu any updates on a fix for this one (or a plan when to fix it)? We are still holding back to v1.23.5 (and the corresponding drivers), and this issue is blocking us from moving forward with new releases. Thanks!

tsipo avatar Nov 04 '22 14:11 tsipo

Duplicated issues: https://github.com/go-gorm/gorm/issues/5838

yyoshiki41 avatar Nov 16 '22 06:11 yyoshiki41