关键字"users_email_unique"的空白字段重复条目“" [英] Duplicate entry '' for key 'users_email_unique' empty field

查看:204
本文介绍了关键字"users_email_unique"的空白字段重复条目“"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种类型的用户-真实用户和假用户. 假用户是不使用系统的员工. 实际用户使用其电子邮件地址登录.因此,我的用户迁移具有$table->string('email')->unique();.

I have two types of users - real and fake. Fake users are employees, that don't use the system. Real users use their email address to login. So my users migration has $table->string('email')->unique();.

问题在于,虚假用户可能没有电子邮件地址.我可以添加第一个假用户没问题,但是第二个假用户会生成错误SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_email_unique'.

The problem is that fake users may not have an email address. I can add first fake user no problem, but the second one generates error SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_email_unique'.

我该怎么办?

推荐答案

users_email_unique这样的声音是您的主键.这意味着,当您使用空白电子邮件地址插入第一个假用户时,空白条目将被计为唯一条目.输入第二个空白条目时,它违反了实体完整性(重复的主键).

Sounds like users_email_unique is your primary key. This means that when you insert your first fake user with a blank email address, the blank entry is counted as a unique entry. When your second blank entry is entered it violates entity integrity (duplicate primary keys).

users_email_unique    unique?
_________________     _______
email@email.com       yes
blank                 yes
blank                 no

如果要多个空白条目,可以允许users_email_uniquenull,但是,主键列不能包含null:

If you want multiple blank entries you could allow users_email_unique to be null, however, a primary key column cannot contain null values:

users_email_unique   unique?
__________________   ________
email@email.com      yes
NULL                 yes
NULL                 yes

如果您使用users_email_unique作为主键,那么,正如注释中指出的那样,您可能需要:

If you are using users_email_unique as your primary key then, as pointed out in the comments, you may need to either:

  • 临时为伪造"用户生成随机的唯一电子邮件
  • 重新考虑表的主键(也许某种形式的唯一ID?)
  • 可能会分为两个表,一个表用于实际"用户,一个表用于伪造"

这篇关于关键字"users_email_unique"的空白字段重复条目“"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆