英文:
Make user email field unique
问题
以下是我翻译好的内容:
以下是我定义的实体:
type User struct {
gorm.Model
FirstName string `json:"first_name" binding:"required"`
LastName string `json:"last_name" binding:"required"`
Email string `json:"email" binding:"required,email" gorm:"unique;not null"`
Phone string `json:"phone" binding:"required"`
Password string `json:"password" binding:"required"`
}
一切都正常工作,但它仍然接受重复的电子邮件。我还尝试了unique_index
,但也没有起作用。
英文:
Following is my entiry defined
type User struct {
gorm.Model
FirstName string `json:"first_name" binding:"required"`
LastName string `json:"last_name" binding:"required"`
Email string `json:"email" binding:"required,email" gorm:"unique,not null"`
Phone string `json:"phone" binding:"required"`
Password string `json:"password" binding:"required"`
}
Every thing work fine, but it still accept duplicated emails. I also tried unique_index
but that isn't working as well.
答案1
得分: 1
你的代码中有一个小错误,
type User struct {
gorm.Model
FirstName string `json:"first_name" binding:"required"`
LastName string `json:"last_name" binding:"required"`
Email string `json:"email" binding:"required,email" gorm:"unique,{Over here}not null"`
Phone string `json:"phone" binding:"required"`
Password string `json:"password" binding:"required"`
}
将unique后面的逗号改为分号,代码就可以正常工作了。
type User struct {
gorm.Model
FirstName string `json:"first_name" binding:"required"`
LastName string `json:"last_name" binding:"required"`
Email string `json:"email" binding:"required,email" gorm:"unique;not null"`
Phone string `json:"phone" binding:"required"`
Password string `json:"password" binding:"required"`
}
英文:
There is a little typo on your code,
type User struct {
gorm.Model
FirstName string `json:"first_name" binding:"required"`
LastName string `json:"last_name" binding:"required"`
Email string `json:"email" binding:"required,email" gorm:"unique,{Over here}not null"`
Phone string `json:"phone" binding:"required"`
Password string `json:"password" binding:"required"`
}
Change the comma after unique to semicolon and it should work.
type User struct {
gorm.Model
FirstName string `json:"first_name" binding:"required"`
LastName string `json:"last_name" binding:"required"`
Email string `json:"email" binding:"required,email" gorm:"unique;not null"`
Phone string `json:"phone" binding:"required"`
Password string `json:"password" binding:"required"`
}
答案2
得分: -1
import "github.com/go-playground/validator/v10"
type User struct {
FirstName string `json:"first_name" validate:"required"`
LastName string `json:"last_name" validate:"required"`
Email string `json:"email" validate:"required,email,unique=Email"`
Phone string `json:"phone" validate:"required"`
Password string `json:"password" validate:"required"`
}
以上是要翻译的内容。
英文:
import "github.com/go-playground/validator/v10"`
type User struct {
FirstName string `json:"first_name" validate:"required"`
LastName string `json:"last_name" validate:"required"`
Email string `json:"email" validate:"required,email,unique=Email"`
Phone string `json:"phone" validate:"required"`
Password string `json:"password" validate:"required"`}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论