skip a field in automigrate GORM

Viewed 755

I have a function in my struct that I want to migrate to the database. Is there a struct tag to prevent gorm from migrating a particular field in the struct?

1 Answers

Ignoring a field altogether

If your struct contains model and non-model members. For example only some of the fields should be read/saved/udpated by Gorm. You can ignore the field altogether (like the json struct tag):

type MyModel struct {
  gorm.Model // Updated by gorm
  Name string // Updated by gorm
  IgnoreMe MyType `gorm:"-"` // Not touched by gorm
}

Not migrating a particular field but still reading/updating the value via gorm

If you change a particular field's type or just don't want to automigrate it. (this is probably a bad idea)

It looks like this was a new feature here: https://github.com/go-gorm/gorm/pull/4028

type MyModel struct {
    gorm.Model
    Name string `gorm:"migration"`

https://github.com/go-gorm/gorm/pull/4028/commits/8771f3c27c1fb73b4b1a21cf706f6d81819b9f41

Related