在GORM中禁用CreatedAt、UpdatedAt和DeletedAt字段。

huangapple go评论70阅读模式
英文:

Disable CreatedAt UpdatedAt DeletedAt Fields in GORM

问题

我正在使用GORM,并且正在映射传统表格。

默认情况下,GORM有以下结构体:

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

但是我在传统表格中没有CreatedAt、UpdatedAt和DeletedAt字段,我需要避免或禁用这个默认结构。

我找不到避免这些列的方法。

英文:

I am usign GORM and I am mapping legacy tables.

By default GORM has this struct:

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

I don't have CreatedAt UpdatedAt DeletedAt fields in the legacy table, I need to avoid or disable this default structure.

I can't find a way to avoid these columns.

答案1

得分: 1

不要翻译代码部分,只返回翻译好的内容:

不要创建嵌入式结构体作为你的模型:

type MyModel struct {
  gorm.Model
  StringField  string
  IntField uint
}

你可以使用你在 gorm.Model 中提到的声明来创建它:

type MyModel struct {
  ID           uint `gorm:"primarykey"`
  StringField  string
  IntField     uint
}
英文:

Instead of creating your model as an embedded struct:

type MyModel struct {
  gorm.Model
  StringField  string
  IntField uint
}

You can create it using the declarations you mentioned in gorm.Model:

type MyModel struct {
  ID           uint `gorm:"primarykey"`
  StringField  string
  IntField     uint
}

答案2

得分: 1

你可以通过在结构体中不指定gorm.Model来避免使用CreatedAtUpdatedAtDeletedAt字段。

你需要在这些结构体字段旁边添加gorm:"column_name"来明确声明它们在Gorm中的样式。

假设你的旧表名为features,并且它有namedescription两列。那么你的结构体应该是这样的:

type Feature struct {
	Name        string `gorm:"column:name"`
	Description string `gorm:"column:description"`
}

或者,如果这个表不仅用于ORM,还用于REST API:

type Feature struct {
	Name        string `json:"name" gorm:"column:name"`
	Description string `json:"description" gorm:"column:description"`
}
英文:

You can avoid fields CreatedAt, UpdatedAt and DeletedAt by not specifying gorm.Model inside the struct.

You have to explicitly declare what these struct fields would look like in Gorm by adding gorm:"column_name" next to them.

Lets assume that your legacy table is called features and it has columns name and description. So your struct would be:

type Feature struct {
	Name        string `gorm:"column:name"`
	Description string `gorm:"column:description"`
}

Or, if this table is used for REST API in addition to ORM:

type Feature struct {
	Name        string `json:"name" gorm:"column:name"`
	Description string `json:"description" gorm:"column:description"`
}

答案3

得分: 0

你可以通过将autoUpdateTime标签设置为false来禁用时间戳跟踪,例如:来源

CreatedAt time.Time gorm:"autoCreateTime:false"
UpdatedAt time.Time gorm:"autoUpdateTime:false"

英文:

You can disable the timestamp tracking by setting autoUpdateTime tag to false, for example: Source

  CreatedAt time.Time `gorm:"autoCreateTime:false"`
  UpdatedAt time.Time `gorm:"autoUpdateTime:false"` 

huangapple
  • 本文由 发表于 2022年9月3日 11:40:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/73589456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定