I have some struct in Go for saving in DB:
type AlarmMessage struct {
ID int64 `json:"id" gorm:"-"`
Description string `json:"description"`
DeviceID int64 `json:"device_id"`
EventCategoryID int64 `json:"event_category_id"`
Ack string `json:"ack"`
AckUserID int64 `json:"ack_user_id"`
AckTM int64 `json:"ack_tm"`
Comments string `json:"comments"`
Severity int64 `json:"severity"`
ActiveTM time.Time `json:"active_tm" gorm:"-"`
AnalogPointID int64 `json:"analog_point_id" gorm:"-"`
DigitalPointID int64 `json:"digital_point_id" gorm:"-"`
InactiveTM time.Time `json:"inactive_tm" gorm:"-"`
Value int64 `json:"value" gorm:"-"`
Border string `json:"border" gorm:"-"`
Icon string `json:"icon" gorm:"-"`
Name string `json:"name" gorm:"-"`
Location string `json:"location" gorm:"-"`
Group string `json:"group" gorm:"-"`
}
But if I try to save it in DB GORM creates invalid query:
INSERT INTO "journal_alarm" ("description","device_id","event_category_id","ack","ack_user_id","ack_tm","comments","severity",") VALUES ('',0,0,'',0,0,'',2,1) RETURNING "
So I got an error:
error while processing alarm: ERROR: syntax error at end of input (SQLSTATE 42601)
The problem is that GORM writes extra quotes and comma after word “severity” and insert ID in values (”,0,0,”,0,0,”,2,1).
I need to ignore the ID field because it is generated by DB.
So my question is: how to fix invalid query? How to make GORM generate a valid query?
Valid query looks like:
INSERT INTO "journal_alarm" ("description","device_id","event_category_id","ack","ack_user_id","ack_tm","comments","severity") VALUES ('',0,0,'',0,0,'',2) RETURNING *