duanfei1930 2019-04-01 07:10
浏览 522

创建或更新未在GORM中返回更新后的值

Following is my model:

type Room struct {
    PK        string `sql:"size:255;not null;"gorm:"primary_key"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time

    LastSentMessageSeq int64
    RoomID             string `sql:"size:255;not null;"gorm:"unique_index:rooms_room_id_client_id"`
    User               User
    UserID             string `sql:"size:255;not null"`
    Client             Client
    ClientID           string `sql:"size:255;not null"gorm:"unique_index:rooms_room_id_client_id"`
}

I am using Postgres. I am trying to do update or create operation. If row exists, increment LastSentMessageSeq field or else insert a new one. Following is my code:

var room = Room{ClientID: clientID, RoomID: roomID, UserID: usedID, LastSentMessageSeq: 10}
err := ws.db.Set("gorm:insert_option", "ON CONFLICT (room_id, client_id) DO UPDATE SET last_sent_message_seq = rooms.last_sent_message_seq + 1").Create(&room).Error
log.Println(err)
log.Println(room)

However, when I print the room object, it does not have the updated value for the field LastSentMessageSeq. But I can see it is doing upsert operation, i.e. it creates a new row if it does not exist or updates the value of LastSentMessageSeq in the database

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大