I'm trying to load queue items from my database. I've created an API endpoint but I can't get the data for the queue item to preload. Instead the entire "data" object is full of empty values.
Handler:
func QueueItemHandler(w http.ResponseWriter, r *http.Request) {
var queueItems QueueItem
var builder = database.Model(QueueItem{})
var queryError = builder.
Preload("Status").
Preload("Data").
Find(&queueItems).
Error
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(queueItems)
}
Here is how my gorm models are set up:
type QueueItemStatus struct {
ID int64 `json:"id" gorm:"primary_key"`
Name string `json:"name"`
Description string `json:"description"`
}
type QueueItemData struct {
ID int64 `json:"id" gorm:"primary_key"`
QueueItemId int64 `json:"queue_item_id"`
ScreenshotUrl string `json:"screenshot_url"`
}
type QueueItem struct {
ID int64 `json:"id" gorm:"primary_key"`
SourceUrl string `json:"source_url"`
OriginId int64 `json:"origin_id"`
StatusId int64 `json:"status_id"`
Status QueueItemStatus `json:"status"`
Data QueueItemData `json:"data" gorm:"foreignKey:id,references:queue_item_id"`
CreatedAt time.Time `json:"created_at"`
}
I'm thinking maybe I have the models set up pointing to the wrong columns if anything?
Try changing this
Data QueueItemData `json:"data" gorm:"foreignKey:QueueItemId;references:ID"`