Support db.G[T]() with Go 1.27 generic methods
Author: crypticpiCreated Sep 1, 2026Updated Sep 1, 2026
Labelstype:feature_request
Describe the feature
Now that Go 1.27 supports generic methods, expose GORM's generic API directly from *gorm.DB:
db.G[User]()(*gorm.DB).G[T] should delegate to the existing gorm.G[T](db, opts...) implementation so both forms have identical behavior and options.
For backwards compatibility:
- Compile the method only for Go 1.27 and newer using a version build constraint.
- Keep
gorm.G[T](db)available and unchanged. - Do not raise GORM's minimum supported Go version.
- Test the receiver method with Go 1.27+ and continue testing the package-level API on older supported Go versions.
Motivation
Before Go 1.27, the language did not permit generic methods, so GORM had to expose this API as a package-level function:
gorm.G[User](db)The receiver-based form is easier to discover, reads naturally in query chains, and avoids repeating both the package name and database argument:
users, err := db.G[User]().
Where("active = ?", true).
Find(ctx)Related Issues
Implementation: #7849
Source: go-gorm/gorm