Let's say I have a gorm.DB object in Go, and I want to extract and assert the query I've built to see it was built correctly. How can I compare the "string" representation of the query to this object ?
Make sure your gorm
is up to date.
ToSQL
example:
sql := DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Model(&User{}).Where("id = ?", 100).Limit(10).Order("age desc").Find(&[]User{})
})
sql //=> SELECT * FROM "users" WHERE id = 100 AND "users"."deleted_at" IS NULL ORDER BY age desc LIMIT 10
DryRun Mode
example:
stmt := db.Session(&Session{DryRun: true}).First(&user, 1).Statement
stmt.SQL.String() //=> SELECT * FROM `users` WHERE `id` = $1 ORDER BY `id`
stmt.Vars //=> []interface{}{1}
Debug
example:
db.Debug().Where("name = ?", "jinzhu").First(&User{})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With