Smart Select Fields
GORM allows select specific fields with Select
, if you often use this in your application, maybe you want to define a smaller struct for API usage which can select specific fields automatically, for example:
type User struct { |
Locking (FOR UPDATE)
GORM supports different types of locks, for example:
DB.Clauses(clause.Locking{Strength: "UPDATE"}).Find(&users) |
Refer Raw SQL and SQL Builder for more detail
SubQuery
A subquery can be nested within a query, GORM can generate subquery when using a *gorm.DB
object as param
db.Where("amount > ?", db.Table("orders").Select("AVG(amount)")).Find(&orders) |
From SubQuery
GORM allows you using subquery in FROM clause with method Table
, for example:
db.Table("(?) as u", DB.Model(&User{}).Select("name", "age")).Where("age = ?", 18}).Find(&User{}) |
Group Conditions
Easier to write complicated SQL query with Group Conditions
db.Where( |
Named Argument
GORM supports named arguments with sql.NamedArg
or map[string]interface{}{}
, for example:
DB.Where("name1 = @name OR name2 = @name", sql.Named("name", "jinzhu")).Find(&user) |
Check out Raw SQL and SQL Builder for more detail
Find To Map
GORM allows scan result to map[string]interface{}
or []map[string]interface{}
, don’t forget to specify Model
or Table
, for example:
var result map[string]interface{} |
FirstOrInit
Get first matched record or initialize a new instance with given conditions (only works with struct or map conditions)
// User not found, initialize it with give conditions |
initialize struct with more attributes if record not found, those Attrs
won’t be used to build SQL query
// User not found, initialize it with give conditions and Attrs |
Assign
attributes to struct regardless it is found or not, those attributes won’t be used to build SQL query and the final data won’t be saved into database
// User not found, initialize it with give conditions and Assign attributes |
FirstOrCreate
Get first matched record or create a new one with given conditions (only works with struct, map conditions)
// User not found, create a new record with give conditions |
Create struct with more attributes if record not found, those Attrs
won’t be used to build SQL query
// User not found, create it with give conditions and Attrs |
Assign
attributes to the record regardless it is found or not and save them back to the database.
// User not found, initialize it with give conditions and Assign attributes |
Optimizer/Index Hints
Optimizer hints allow to control the query optimizer to choose a certain query execution plan, GORM supports it with gorm.io/hints
, e.g:
import "gorm.io/hints" |
Index hints allow passing index hints to the database in case the query planner gets confused.
import "gorm.io/hints" |
Refer Optimizer Hints/Index/Comment for more details
Iteration
GORM supports iterating through Rows
rows, err := db.Model(&User{}).Where("name = ?", "jinzhu").Rows() |
FindInBatches
Query and process records in batch
// batch size 100 |
Query Hooks
GORM allows hooks AfterFind
for a query, it will be called when querying a record, refer Hooks for details
func (u *User) AfterFind(tx *gorm.DB) (err error) { |
Pluck
Query single column from database and scan into a slice, if you want to query multiple columns, use Select
with Scan
instead
var ages []int64 |
Scopes
Scopes
allows you to specify commonly-used queries which can be referenced as method calls
func AmountGreaterThan1000(db *gorm.DB) *gorm.DB { |
Checkout Scopes for details
Count
Get matched records count
var count int64 |