Gorm 多个事务以删除旧的搜索条件


    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 删除日期时间条件。第二个 SQL 采用第一个 SQL 搜索条件。如何删除此条件并在事务中使用它?

我建议有两个单独的查询对象:

    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()

相关内容

  • 没有找到相关文章

最新更新