GORM的official documentation说明了一种可以测试记录是否存在的方法,即:
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
这可用于间接测试记录创建过程中的错误,但在发生故障时不报告任何有用信息.
在判断了source code for db.Create
之后,似乎有某种堆栈帧判断,在继续之前判断错误,这意味着事务性错误将silent地失败:
func Create(scope *Scope) {
defer scope.Trace(NowFunc())
if !scope.HasError() {
// actually perform the transaction
}
}
- 这是个窃听器,还是我漏掉了什么?
- How can/should I be informed of a failed transaction?个
- 在哪里可以获得有用的调试信息?