postgresql postgres.Jsonb


//postgres.Jsonb 处理
type homeWorkQuestionIds struct {
		QuestionIDs           postgres.Jsonb `json:"question_ids"`   
	}

var homeworkRes []homeWorkQuestionIds

db.Model(&lesson.LessonHomeWork{}).
		Select("question_ids").
		Joins("LEFT JOIN xxxx on lesson_home_works.home_work_id = home_works.id").
		Where("ccc.lesson_id = ?", lessonIDQuery).
		Scan(&homeworkRes)

var total []uint
	var request []questionListRequest
	for i := range homeworkRes{
		var questionIds []uint
		json.Unmarshal(homeworkRes[i].QuestionIDs.RawMessage,&questionIds)
		total = append(total,questionIds...)
	}

 jsonb_array_length ( home_works.question_ids )

 

type HomeWork struct {
    QuestionIDs           postgres.Jsonb `json:"question_ids"`   

}


type homeWorkRequest struct {

    QuestionIDs  []uint `json:"question_ids"`  

}

questionIDsByte, err := json.Marshal(request.QuestionIDs)
homeWorkModel.QuestionIDs = postgres.Jsonb{RawMessage: questionIDsByte}

 

posted @ 2022-01-05 15:09  hubb  阅读(29)  评论(0编辑  收藏  举报