英文:
How to write a migration to add not null in table field GOLANG
问题
我想修改User模型中的一个列。
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)"`
}
我想将Email字段设置为非空。如何编写迁移来实现这个目标?
英文:
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?
答案1
得分: 1
在标签gorm上添加not null
type User struct {
...
Email string `gorm:"unique;type:varchar(50);not null"`
...
}
文档:https://gorm.io/docs/models.html
英文:
add not null
on tag gorm
type User struct {
...
Email string `gorm:"unique;type:varchar(50);not null"`
...
}
答案2
得分: 0
编辑你的模型,然后使用AutoMigrate
函数。
db.AutoMigrate(&User{})
文档:https://gorm.io/docs/migration.html
英文:
Edit your model, then us AutoMigrate
db.AutoMigrate(&User{})
Documentation: https://gorm.io/docs/migration.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论