首页 > 解决方案 > 如何解析复合结构中的表

问题描述

我有几个结构,其中一个包含所有其余结构。我需要使用 GORM 来获取主要结构。

我尝试使用带有“列”的 gorm 标签,但不起作用

package database

import (
    "database/sql"
    "time"
)

type TaskItem struct {
    Ind              int32          `gorm:"column:Ind"`
    ParentIndArray   sql.NullString `gorm:"column:ParentIndArray"`
    ParentInd        int32          `gorm:"column:ParentInd"`
    SourceId         string         `gorm:"column:SourceId"`
    SourceInd        int32          `gorm:"column:SourceInd"`
    ContentIndActive int32          `gorm:"column:ContentIndActive"`
    StatusIndInit    int32          `gorm:"column:StatusIndInit"`
    StatusIndActive  int32          `gorm:"column:StatusIndActive"`
    TaskTypeInd      int32          `gorm:"column:TaskTypeInd"`
    RootTaskTypeInd  int32          `gorm:"column:RootTaskTypeInd"`
    FieldInd         int32          `gorm:"column:FieldInd"`
    StateIndActive   int32          `gorm:"column:StateIndActive"`
    ChildIndArray    []byte         `gorm:"column:ChildIndArray"`
}

type TaskContent struct {
    Ind               int32     `gorm:"column:ContentInd"`
    TaskInd           int32     `gorm:"-"`
    SessionInd        int32     `gorm:"column:ContentSessInd"`
    Caption           string    `gorm:"column:Caption"`
    Content           []byte    `gorm:"column:Content"`
    Priority          int32     `gorm:"column:Priority"`
    PlanningDateStart time.Time `gorm:"column:PlanningDateStart"`
    PlanningDateEnd   time.Time `gorm:"column:PlanningDateEnd"`
    TimeEstimate      time.Time `gorm:"column:TimeEstimate"`
    ApplyDate         time.Time `gorm:"column:ContentApplyDate"`
}

type TaskStatus struct {
    Ind         int32     `gorm:"column:StatusInd"`
    TaskInd     int32     `gorm:"-"`
    ChainId     string    `gorm:"column:ChainId"`
    MessageId   string    `gorm:"column:MessageID"`
    SessionInd  int32     `gorm:"column:StatusSessInd"`
    StatusInd   int32     `gorm:"column:Status"`
    Text        string    `gorm:"column:Text"`
    Comment     string    `gorm:"column:Comment"`
    ApplyDate   time.Time `gorm:"column:StatusApplyDate"`
    ReceiveData time.Time `gorm:"column:StatusReceiveDate"`
}

type TaskState struct {
    Ind        int32     `gorm:"column:StateInd"`
    TaskInd    int32     `gorm:"-"`
    SessionInd int32     `gorm:"column:StateSessInd"`
    State      int32     `gorm:"column:State"`
    ApplyDate  time.Time `gorm:"column:StateApplyDate"`
}

type Task struct {
    Item    TaskItem
    Content TaskContent
    Status  TaskStatus
    State   TaskState
}

我希望 Task 包含来自数据库的其他数据。我应该怎么办?

笔记。起初,我使用:

// tasks is equal []Task
err = database.db.Raw("CALL pr_task_list_by_account_ind(?)", accountGroupInd).Scan(&tasks)

然后我决定尝试使用:

var ti []TaskItem
var tc []TaskContent
var tse []TaskState
var tss []TaskStatus
err = database.db.Raw("CALL pr_task_list_by_account_ind(?)", accountGroupInd).Scan(&ti).Scan(&tc).Scan(&tse).Scan(&tss).Error

我注意到第一个扫描仪可以工作,如果我删除它,那么第二个扫描仪也开始工作

标签: mysqlgogo-gorm

解决方案


推荐阅读