在gorm中获取结构中的嵌套对象

Kir*_*sko 6 postgresql go go-gorm

我有两个结构:

type GoogleAccount struct {
     Id     uint64
     Token  string
}
Run Code Online (Sandbox Code Playgroud)

它代表我的自定义 PostgreSQL 对象类型(我自己创建):

CREATE TYPE GOOGLE_ACCOUNT AS
(
  id    NUMERIC,
  token TEXT
);
Run Code Online (Sandbox Code Playgroud)

下一个结构是数据库中的表:

type Client struct {
     IdClient       uint64          `gorm:"primary_key"`
     Name           string
     PhotoUrl       string
     ApprovalNumber uint16
     Phone          string
     Password       string
     HoursOfNotice  int8
     Google         GoogleAccount
}
Run Code Online (Sandbox Code Playgroud)

我的自定义对象嵌套在类型 Client 中并命名为google. 我尝试通过以下方式读取数据:

var users model.Client
db.First(&users)
Run Code Online (Sandbox Code Playgroud)

但不幸的是我无法读取字段google(具有默认值)。我不想使用 google_account 创建单独的表,或者将此结构作为客户端表中的单独字段或将其打包为 json(创建单独的实体,因为此结构不仅在此表中使用,而且我正在寻找新方法,即得到相同的结果,但更优雅)。任务不是简化表中数据的表示。我需要将对象从 postgres正确映射到实体。

现在我找到了一种解决方案 - 将 Scanner 实施到 GoogleAccount。但是输入法中的值是[]uint8。正如我所假设的, []uint8 可以转换为字符串,然后我可以解析这个字符串。这个字符串(保留在 db 中)看起来像(x,x)- 其中 x - 是值。解析字符串并将值设置为对象是正确的方法吗?或者是通过ORM获得这个结果的方法?

是否有可能将此数据作为嵌套结构对象读取?

Jon*_*han 8

看起来您需要用您拥有的东西做两件事:(1) 更新模型,以便您拥有正确的关系绑定,以及 (2).Preload()如果您试图让它关联数据,请使用该方法读。

模型变化

Gorm 会根据结构中的属性名称和引用结构的名称自动推断关系。问题是Googletype 的属性GoogleAccount没有关联,因为 gorm 正在寻找type Google struct.

您还缺少GoogleAccount. ORM 如何知道哪个GoogleAccount与哪个关联Client?您应该将 a 添加ClientId到您的GoogleAccount结构定义中。

另外,我会更改您用来键入的主键,uint因为这是 gorm 默认设置的(除非您有充分的理由不使用它)

如果我是你,我会将结构定义更改为以下内容:

type Client struct {
     IdClient       uint            `gorm:"primary_key"`
     Name           string
     PhotoUrl       string
     ApprovalNumber uint16
     Phone          string
     Password       string
     HoursOfNotice  int8
     GoogleAccount  GoogleAccount    // Change this to `GoogleAccount`, the same name of your struct
}

type GoogleAccount struct {
     Id             uint
     ClientId       uint             // Foreign key
     Token          string
}
Run Code Online (Sandbox Code Playgroud)

有关这方面的更多信息,请查看此处的关联文档:http : //gorm.io/associations.html#has-one

预加载关联

现在您实际上已经正确关联了它们,您可以获得.Preload()所需的嵌套对象:

db.Preload("GoogleAccount").First(&user)
Run Code Online (Sandbox Code Playgroud)

使用.Preload()将填充user.GoogleAccount与正确关联属性的GoogleAccount基础上ClientId

有关这方面的更多信息,请查看预加载文档:http : //gorm.io/crud.html#preloading-eager-loading


Kir*_*sko 3

现在我找到了一个解决方案 - 将Scanner实现为GoogleAccount. 在输入Scan我得到的方法时[]uint8,我将其转换为字符串并最终进行解析。这个字符串(保存在数据库中)看起来像(x,x)- 其中x- 是值。当然,这不是实现我的目标的正确方法。但我找不到其他解决方案。

强烈建议使用经典的关系绑定或简单地将这些字段作为最简单的值(而不是对象)保留在表中。

但是如果你想尝试表中的嵌套对象,你可以看看我的实现,也许它对你有用:

type Client struct {
    // many others fields
    Google          GoogleAccount   `json:"google"`
}

type GoogleAccount struct {
    Id      uint64      `json:"id"`
    Token   string      `json:"token"`
}

func (google GoogleAccount) Value() (driver.Value, error) {
    return "(" + strconv.FormatUint(google.Id, 10) + "," + google.Token + ")", nil
}

func (google *GoogleAccount) Scan(value interface{}) error {
    values := utils.GetValuesFromObject(value)
    google.Id, _ = strconv.ParseUint(values[0], 10, 64)
    google.Token = values[1]
    return nil
}
Run Code Online (Sandbox Code Playgroud)

  • @LucioMollinedo 感谢您的评论。我添加了代码片段。 (2认同)