重复条目 ''对于密钥 'users_email_unique'空地 [英] Duplicate entry '' for key 'users_email_unique' empty field

查看:63
本文介绍了重复条目 ''对于密钥 '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 conflict: 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天全站免登陆