Skip to content

loss of "stmt.Clauses" when calling the scopes function #34

@raisons

Description

@raisons

Description

// models.User
type User struct {
	ID       uint `gorm:"primarykey"`
	Name     string
}
// g.User
var User = struct {
	ID       field.Number[uint]
	Name     field.String
}{
	ID:       field.Number[uint]{}.WithColumn("id"),
	Name:     field.String{}.WithColumn("name"),
}
// scope func
func ScopeTest(stmt *gorm.Statement) {
	fmt.Println(stmt.Clauses)
}

func main() {
	db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	err = db.AutoMigrate(&models.User{})
	if err != nil {
		panic(err)
	}
    // Call the Scopes function first, ScopeTest func will print "map[]"
	_, _ = gorm.G[models.User](db).Scopes(ScopeTest).Where(g.User.Name.Eq("11111")).Take(context.Background())

    // Move the Scopes function to after the Where clause, it will work normally.
    _, _ = gorm.G[models.User](db).Where(g.User.Name.Eq("11111")).Scopes(ScopeTest).Take(context.Background())
   
}

As shown in the code, when the Scopes function is called at the very beginning, the scope function will obtain an empty stat.Clauses

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions