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?
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?
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
}
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