Home > other >  Does AutoMigration() also give the NOT NULL attribute on the database side?
Does AutoMigration() also give the NOT NULL attribute on the database side?

Time:12-04

In GORM, does AutoMigration() also give the NOT NULL attribute on the database side?

Thanks in advance

CodePudding user response:

The answer is: No

So if you do not define not null (using GORM field tags) to that particular field, GORM will not add NOT NULL constraint to the field on db side. Except for the primary key. By default, PK will be defined as NOT NULL field.

Way to define the field as NOT NULL in GORM:

type User struct {
    ...
    Email string `gorm:"not null"` // NOT NULL 
    ...
}

For more, see official documentation of GORM: Field Tags

  • Related