Home > Back-end >  disable CreatedAt UpdatedAt DeletedAt GORM
disable CreatedAt UpdatedAt DeletedAt GORM

Time:09-03

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 don't find a way to avoid this columns.

Thank you.

CodePudding user response:

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
}

CodePudding user response:

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"` 
  • Related