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

WBOY
Release: 2024-02-05 23:21:11
forward
828 people have browsed it

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 
    ...
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!