go-gorm / go-gorm/cli

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

Open
#34 0 comments 0 reactions 1 assignee Claimed by @jinzhu View on GitHub
Dominant language
Go
Stars
108
Forks
15
PR merge metrics
No merged PRs in 30d

Description

## Description

```go
// models.User
type User struct {
ID uint `gorm:"primarykey"`
Name string
}
```

```go
// g.User
var User = struct {
ID field.Number[uint]
Name field.String
}{
ID: field.Number[uint]{}.WithColumn("id"),
Name: field.String{}.WithColumn("name"),
}

```

```go
// 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`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.