首页 > 解决方案 > 如何在 go 的 gorm 模型中访问每个字段需要映射到的 postgres 类型?

问题描述

假设我有一个模型

type MyModel struct {
  FirstField    *CustomType  `gorm:"primaryKey"`
  SecondField   string       `gorm:"type:varchar(42)"`
}

type CustomType struct {
    *big.Int
}

func (CustomType) GormDataType() string {
    return "numeric"
}

有什么方法可以通过 db.DB 对象找出 FirstField 的 postgres 类型应该是数字,SecondField 的 varchar(42) 类型应该是数字吗?

我注意到表单中的每个字段都有一个在 schema/field.go ParseField 中解析的 DataType。它在哪里被调用,它是否存储在数据库中的某个地方?我注意到我需要一个模式来自己调用 ParseField,但 db 模式似乎为零。我错过了什么吗?谢谢!

标签: postgresqlgo-gorm

解决方案


推荐阅读