douzi1991 2018-09-08 20:37
浏览 1407
已采纳

使用gorm模型的嵌套结构

I have struct called User:

type User struct {
    Email string
    Name string
}

and struct called UserDALModel:

type UserDALModel struct {
    gorm.Model
    models.User
}

gorm Model looks like this:

type Model struct {
    ID        uint `gorm:"primary_key"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time `sql:"index"`
}

this is possible to make UserDALModel nested with gorm model and user model so the output will be:

{
    ID
    CreatedAt
    UpdatedAt
    DeletedAt
    Email 
    Name
}

now the output is:

{
    Model: {
        ID
        CreatedAt
        UpdatedAt
        DeletedAt
    }
    User: {
        Name
        Email
    }
}
  • 写回答

2条回答 默认 最新

  • dqy27359 2018-09-09 13:46
    关注

    I found the answer:

    type UserModel struct {
        Email string
        Name string
    }
    
    type UserDALModel struct {
        gorm.Model
        *UserModal
    }
    
    ------------------------------
    
    user := UserModel{"name", "email@email.com"}
    userDALModel := UserDALModel{}
    userDal.UserModal = &user
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序