使用户的电子邮件字段唯一

huangapple go评论85阅读模式
英文:

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"`}

huangapple
  • 本文由 发表于 2022年9月13日 19:13:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/73701967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定