GORM的官方文档展示了一种可以测试记录是否存在的方法,即:
user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}
// returns true if record hasn’t been saved (primary key `Id` is blank)
db.NewRecord(user) // => true
db.Create(&user)
// will return false after `user` created
db.NewRecord(user) // => false
这可用于间接测试记录创建中的错误,但在发生故障时不会报告有用的信息。
在检查了源代码db.Create
之后,似乎有某种堆栈帧检查可以在继续之前检查错误,这意味着事务错误将静默失败:
func Create(scope *Scope) {
defer scope.Trace(NowFunc())
if !scope.HasError() {
// actually perform the transaction
}
}
- 这是一个错误,还是我错过了什么?
- 我如何/应该被告知交易失败?
- 我在哪里可以获得有用的调试信息?