我有一个查询,其中2个连接到其他表,查询执行并返回主表中的所有字段,但我不知道如何访问连接的字段。
在func LastTest()中我将var 结果设置为TestResultDetail表,我还希望访问结果中的TestResult和SeqTestStage值,这些值在查询中加入。
发布查询我可以访问TestResult表中的所有结果字段,但我没有访问任何表。
TestResultDetail与TestResult有FK关系。 单个TestResult记录有许多TestResultDetail记录。
例如,seq_test_stage.description或seq_test_stage中的任何字段都会显示为空。
main.go
package main
import (
"./models"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
func main() {
router := gin.Default()
opt := router.Group("opt/v2")
{
opt.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
opt.GET("/last-completed-test/:serial", LastTest)
}
// Add API handlers here
router.Run(":3000")
}
func LastTest(c *gin.Context) {
// Connection to the database
db := models.InitDB()
defer db.Close()
var result models.TestResultDetail
type Response struct {
Status string
Id int64
Serial string
Product string
Stage string
}
// get param and query
serial := c.Params.ByName("serial")
db.Model(&models.TestResultDetail{}).
Select("test_result.id, test_result.serial, test_result.product, test_result_detail.stage_id, seq_test_stage.description").
Joins("join test_result on test_result.ID = test_result_detail.result_id").
Joins("join seq_test_stage on seq_test_stage.ID = test_result_detail.stage_id").
Where("test_result.serial = ?", serial).
Order("test_result_detail.id desc").
Limit(1).
Scan(&result)
if result.ID != 0 {
res1 := &Response{
Status: "OK",
Id: result.ResultId.ID,
Serial: result.ResultId.Serial,
Product: result.ResultId.Product,
Stage: result.StageId.Description,
}
c.JSON(200, res1)
} else {
// Display JSON error
c.JSON(404, gin.H{"error": "No Records", "code": 404})
}
}
test_result_detail.go
package models
import (
"time"
)
type TestResultDetail struct {
ID int64 `gorm:"primary_key" db:"id" json:"id"`
ResultId TestResult
StatusId ResultStatus
StationId Station
StageId SeqTestStage
OperatorId AuthUser
Failstep string `db:"fail_step" json:"fail_step"`
Shift string `db:"shift" json:"shift"`
SequenceRev int `db:"sequence_rev" json:"sequence_rev"`
DateAdded time.Time `db:"date_added" json:"date_added"`
DateTimestamp time.Time `db:"date_timestamp" json:"date_timestamp"`
DateTime time.Time `db:"date_time" json:"date_time"`
StageOrder int `db:"stage_order" json:"stage_order"`
SerialNumber string `db:"serial_number" json:"serial_number"`
IsRetest int `db:"is_retest" json:"is_retest"`
RetestReason string `db:"retest_reason" json:"retest_reason"`
}
test_result.go
package models
import (
"time"
)
type TestResult struct {
ID int64 `gorm:"primary_key" db:"id" json:"id"`
DateAdded time.Time `db:"date_added" json:"date_added"`
Serial string `db:"serial" json:"serial"`
SequenceId int `db:"sequence_id" json:"sequence_id"`
LastCompletedStage int `db:"last_completed_stage" json:"last_completed_stage"`
LastCompletedSequence int `db:"last_completed_sequence" json:"last_completed_sequence"`
Workorder string `db:"workorder" json:"workorder"`
Product string `db:"product" json:"product"`
IsComplete string `db:"is_complete" json:"is_complete"`
IsScrapped string `db:"is_scrapped" json:"is_scrapped"`
ValueStream string `db:"value_stream" json:"value_stream"`
PromiseDate string `db:"promise_date" json:"promise_date"`
FailLock int `db:"fail_lock" json:"fail_lock"`
SequenceRev int `db:"sequence_rev" json:"sequence_rev"`
DateUpdated time.Time `db:"date_updated" json:"date_updated"`
Date time.Time `db:"date" json:"date"`
Time time.Time `db:"time" json:"time"`
Ptyp2 string `db:"ptyp2" json:"ptyp2"`
WoQty int `db:"wo_qty" json:"wo_qty"`
IsActive int `db:"is_active" json:"is_active"`
IsTimeLock int `db:"is_time_lock" json:"is_time_lock"`
TimeLockTimestamp time.Time `db:"time_lock_timestamp" json:"time_lock_timestamp"`
ScrapReason string `db:"scrap_reason" json:"scrap_reason"`
ScrappedBy int `db:"scrapped_by" json:"scrapped_by"`
}
seq_test_stage.go
package models
import (
"time"
)
type SeqTestStage struct {
ID int64 `gorm:"primary_key" db:"id" json:"id"`
PdcptypeId int `db:"pdcptype_id" json:"pdcptype"`
Description string `db:"description" json:"description"`
LongDescription string `db:"long_description" json:"long_description"`
IsActive int `db:"is_active" json:"is_active"`
DateAdded time.Time `db:"date_added" json:"date_added"`
DateUpdated time.Time `db:"date_updated" json:"date_updated"`
TimeLock int `db:"time_lock" json:"time_lock"`
LockMinutes int `db:"lock_minutes" json:"lock_minutes"`
LockHours int `db:"lock_hours" json:"lock_hours"`
}