我在使用gin框架开发的golang应用程序中出错,我输了1天,但是我不明白我写错的地方,因为enquiry-564不是重复的。这是当前错误:
{[{"body": {"text": ""}, "enquiryID": 564, "meta": {"message": {"enquiry_offer": {"delivery_time": 1, "revision_count": 3, "_service_title": "c c sharp cpp java vb work", "price": 1100, "expired_on": "2018-07-21T17:02:29Z", "id": 82, "_service_id": "lBdHu0"}}}, "userID": 1, "type": "enquiry_offer"}]}[GIN] 2018/07/18 - 17:02:28 | 500 | 1.597772ms | 2a01:7e01:0:0:f03c:91ff:fe3e:826a | POST /api/private/message/enquiry
错误#01:错误1062:密钥重复输入“ enquiry-564” 'type_entity_id'
[GIN] 2018/07/18 - 17:02:28 | 500 | 1.659839ms | 2a01:7e01:0:0:f03c:91ff:fe3e:826a | POST /api/private/message/enquiry
Error #01: Error 1062: Duplicate entry 'enquiry-564' for key 'type_entity_id'
这是接收到的json数据:
{
[
{
"body": {
"text": ""
},
"enquiryID": 564,
"meta": {
"message": {
"enquiry_offer": {
"delivery_time": 1,
"revision_count": 3,
"_service_title": "c c sharp cpp java vb work",
"price": 1100,
"expired_on": "2018-07-21T17:02:29Z",
"id": 82,
"_service_id": "lBdHu0"
}
}
},
"userID": 1,
"type": "enquiry_offer"
}
]
}
这是完整的功能代码:
func (p *PrivateRouter) postMessageEnquiry(c *gin.Context) {
logRequestBody(c)
type Message struct {
EnquiryId int64 `json:"enquiryID" binding:"required"`
UserID int64 `json:"userID"`
Type string `json:"type" binding:"required"`
Users []int64 `json:"users" binding:"required"`
Body struct {
Text string `json:"text" binding:"required"`
} `json:"body" binding:"required"`
Meta struct {
Room interface{} `json:"room"`
Message interface{} `json:"message"`
} `json:"meta"`
}
type MessagesRequest []*Message
// this function need for debug
fmt.Printf("%s", c.Request.Body)
/// read either one entity or array of entities from JSON request
isArr, err := requestIsJsonArray(c)
if err != nil {
responseInternalError(c, err)
return
}
var messages MessagesRequest
if isArr {
err = c.Bind(&messages)
if err != nil {
return
}
} else {
messages = make(MessagesRequest, 0)
var message Message
err = c.Bind(&message)
if err != nil {
return
}
messages = append(messages, &message)
}
///
for _, message := range messages {
room, err := p.roomRepo.FindOneWithUsers(model.RoomTypeEnquiry, message.EnquiryId, message.Users)
if err != nil {
responseInternalError(c, err)
return
}
if room == nil {
roomMetaJson, err := json.Marshal(message.Meta.Room)
if err != nil {
responseInternalError(c, err)
return
}
room, err = p.roomRepo.AddEnquiryRoom(message.EnquiryId, roomMetaJson, message.Users)
if err != nil {
responseInternalError(c, err)
return
}
}
roomStringId := model.GenerateRoomStringId(model.RoomTypeEnquiry, message.EnquiryId)
if message.Type == "" {
message.Type = "message"
}
err = p.createMessage(room.Id, roomStringId, message.Type, message.Meta.Message, message.Body.Text, message.UserID)
if err != nil {
responseInternalError(c, err)
return
}
}
sendOkResponse(c)
}
谁能告诉我我错了?
用于存储在数据库中的sql函数
func (rep *MessagesRepository) Add(roomId int64, messageType string, metaJson []byte, message string, senderId int64) (*Message, error) {
var err error
var res sql.Result
cols := []string{"room_id", "type", "message"}
args := []interface{}{roomId, messageType, message}
if string(metaJson) != "null" {
cols = append(cols, "meta_json")
args = append(args, metaJson)
}
if senderId > 0 {
cols = append(cols, "user_id")
args = append(args, senderId)
}
q := `INSERT INTO messages (` + strings.Join(cols, ", ") + `) VALUES (?` + strings.Repeat(", ?", len(cols)-1) + `)`
res, err = rep.db.Exec(q, args...)
if err == nil {
messageId, err := res.LastInsertId()
if err == nil {
return rep.FindByPk(messageId)
}
}
return nil, err
}