Home > front end >  How to write a migration to add not null in table field GOLANG
How to write a migration to add not null in table field GOLANG

Time:05-18

I want to alter a column in User Model

type User struct {
    gorm.Model
    Email         string `gorm:"unique;type:varchar(50)"`
    Password      string
    Active        bool
    FirstName     string `gorm:"type:varchar(10)"`
    LastName      string `gorm:"type:varchar(10)"`
    Age           int
    Gender        bool   `gorm:"type:boolean"`
    MaritalStatus bool   `gorm:"type:boolean"`
    Address       string `gorm:"type:varchar(10)"`
    City          string `gorm:"type:varchar(10)"`
    State         string `gorm:"type:varchar(10)"`
    Country       string `gorm:"type:varchar(10)"`
    ContactNoOne  string `gorm:"type:varchar(13)"`
    ContactNoTwo  string `gorm:"type:varchar(13)"`
}

I want to make Email field as not nullable. How to write migration for that?

CodePudding user response:

Edit your model, then us AutoMigrate

db.AutoMigrate(&User{})

Documentation: https://gorm.io/docs/migration.html

CodePudding user response:

add not null on tag gorm

type User struct {
    ...
    Email         string `gorm:"unique;type:varchar(50);not null"`
    ...
}

doc : https://gorm.io/docs/models.html

  • Related