bg := Db.Begin()
UDebt := make([]UserDebt, 0)
page, _ := strconv.Atoi(c.DefaultPostForm("page", "1"))
limit, _ := strconv.Atoi(c.DefaultPostForm("limit", "20"))
db := Db.Model(&UDebt).Preload("User")
start := c.PostForm("start")
if start != "" {
db = db.Where("datetime >= ?", start)
bg = bg.Where("datetime >= ?", start)
}
debts := make([]UserDebt,0)
bg.Debug().Set("gorm:query_option", "FOR UPDATE").Limit(limit).Offset(page).Find(&debts)
// show sql: SELECT * FROM `user_debt` WHERE (datetime >= '2019-06-16 00:00:00') LIMIT 20 OFFSET 1 FOR UPDATE
// I hope this is a new connection without any conditions.
bg.Debug().Model(&UserBet{}).Where("id in (?)",arrayID).Update("is_read",1)
// show sql: UPDATE `user_bet` SET `is_read` = '1' WHERE (datetime >= '2019-06-16 00:00:00') AND (id in ('17','18','19','20','21','22'))
bg.Commit()
我希望第二个SQL删除datetime条件。 第二个SQL采用第一个SQL搜索条件。如何删除此条件并将其用于交易中?
答案 0 :(得分:0)
我建议使用两个单独的查询对象:
bg := Db.Begin()
UDebt := make([]UserDebt, 0)
page, _ := strconv.Atoi(c.DefaultPostForm("page", "1"))
limit, _ := strconv.Atoi(c.DefaultPostForm("limit", "20"))
// Use the bg object so this is all done in the transaction
db := bg.Model(&UDebt).Preload("User")
start := c.PostForm("start")
if start != "" {
// Don't change the original bg object
db = db.Where("datetime >= ?", start)
}
debts := make([]UserDebt,0)
// Use the newly created db object to store the query options for that
db.Debug().Set("gorm:query_option", "FOR UPDATE").Limit(limit).Offset(page).Find(&debts)
bg.Debug().Model(&UserBet{}).Where("id in (?)",arrayID).Update("is_read",1)
bg.Commit()