代码之家  ›  专栏  ›  技术社区  ›  rluisr

选择更新位置错误的位置

  •  -1
  • rluisr  · 技术社区  · 7 年前

    预期此代码结果为 null 但我没有得到空值。
    rDB.Where("user_id = ? AND updated_at > ?", userID, date).Find(&onedays)

    date := "2018-01-04 23:18:00" 还有一些记录 Onedays 桌子

    +----+---------+------------+------------+---------------------+
    | id | user_id | save_state | date       | updated_at          |
    +----+---------+------------+------------+---------------------+
    |  1 |      44 |          0 | 1514214001 | 2018-01-04 21:25:05 |
    |  2 |      44 |          0 | 1514214001 | 2018-01-04 22:07:55 |
    |  3 |      15 |          1 | 1514300400 | 2018-01-04 23:17:49 |
    +----+---------+------------+------------+---------------------+
    

    返回其代码为:

    {
    "onedays": [
        {
            "id": 1,
            "user_id": 44,
            "save_state": false,
            "date": 1514214001,
            "updated_at": "2018-01-04T21:25:05+09:00"
        },
        {
            "id": 2,
            "user_id": 44,
            "save_state": false,
            "date": 1514214001,
            "updated_at": "2018-01-04T22:07:55+09:00"
        }
    ],
    "photos": null
    }
    

    但是我执行这个查询,return是空的。
    SELECT * FROM Onedays WHERE user_id = 44 AND updated_at > '2018-01-04 23:18:00'

    此问题可能是由gorm设置引起的。
    我怎样才能解决这个问题?

    更新粘贴的功能代码

    func getDiff(userID, date string) interface{} {
        var wg sync.WaitGroup
    
        var onedays []model.OnedayDiff
        var photos []model.PhotoDiff
        var resPhotos []model.PhotoDiff
    
        _, rDB := lib.DB()
        rDB.Where("user_id = ? AND updated_at > ?", userID, date).Find(&onedays)
    
        funcs := []func(){
            // Photo
            func() {
                rDB.Where("user_id = ?", userID).Find(&onedays)
                for _, v := range onedays {
                    rDB.Where("oneday_id = ? AND updated_at > ?", v.ID, date).Find(&photos)
                    resPhotos = append(resPhotos, photos...)
                }
            },
        }
        for _, sleep := range funcs {
            wg.Add(1)
    
            go func(function func()) {
                defer wg.Done()
                function()
            }(sleep)
        }
        wg.Wait()
    
        return map[string]interface{}{
            "onedays": onedays,
            "photos":  resPhotos,
        }
    }
    

    photos 是正确的。 onedays 不正确。。。

    2 回复  |  直到 6 年前
        1
  •  1
  •   leaf bebop    7 年前

    在这里:

    func() {
        rDB.Where("user_id = ?", userID).Find(&onedays)
        for _, v := range onedays {
            rDB.Where("oneday_id = ? AND updated_at > ?", v.ID, date).Find(&photos)
            resPhotos = append(resPhotos, photos...)
        }
    },
    

    你重复使用了切片 onedays 在第二行,所以它不再是空的。在第四行中,你写信给 photos 异步,这会导致数据争用,如果有多个goroutine,则会导致混乱。

    应在func中重新声明两个数组:

    func() {
        var onedays []model.OnedayDiff
        var photos []model.PhotoDiff
        //other codes
    }
    
        2
  •  0
  •   peiiion    7 年前

    正如您从GORM获得的响应中所看到的 update_at 值包含时区(包括 +09:00 部分),而数据库中的值则没有,因此这些值要么作为 UTC 或作为数据库服务器中配置的默认时区。

    您应该检查DB和运行代码的主机之间的时区差异。