Home  >  Article  >  Backend Development  >  Does AutoMigration() also give NOT NULL attribute on database side?

Does AutoMigration() also give NOT NULL attribute on database side?

WBOY
WBOYforward
2024-02-05 23:21:11830browse

AutoMigration() 是否也在数据库端给出 NOT NULL 属性?

Question content

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

Thanks in advance


Correct answer


The answer is: No

So if you don't define not null (using the gorm field tag) to that specific field, gorm will not add the not null constraint to the field on the database side. Except for primary keys. By default, pk will be defined as a not null field.

How to define a field as not null in gorm:

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

For more information, please refer to gorm official documentation: Field tags p>

The above is the detailed content of Does AutoMigration() also give NOT NULL attribute on database side?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete