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