问题描述
在PG中:
我制作了一个包含唯一电子邮件的用户表,但后来决定电子邮件不应该是唯一的。我进行了更改以使我的电子邮件字段不唯一(我使用ORM,因此实际上没有发生的确切SQL),但是PG仍然不允许我使用重复的电子邮件地址。
I made a user table that includes unique emails, but later decided that emails should not be unique. I pushed changes to make my email field non-unique (I use an ORM, so I don't actually have the exact SQL that took place), but PG still won't let me use duplicate email addresses.
我检查了索引,它不是唯一的,但是有一个约束条件,使我不能拥有重复的电子邮件地址。但是,我在删除此约束时遇到了麻烦。我在做什么错?
I checked the index and it's not unique, but there's a constraint keeping me from having duplicate email addresses. However I'm having trouble dropping this constraint. What am I doing wrong?
SQL> ALTER TABLE "users" DROP CONSTRAINT "unique_users_email"
PGError: ERROR: constraint "unique_users_email" of relation "users" does not exist
SQL> UPDATE users SET email = '[email protected]'
PGError: ERROR: duplicate key value violates unique constraint "unique_users_email"
DETAIL: Key (email)=([email protected]) already exists.
推荐答案
我敢打赌, unique_users_email实际上是唯一的索引,而不是约束。试试:
I bet that "unique_users_email" is actually the name of a unique index rather than a constraint. Try:
DROP INDEX unique_users_email;
当查看表的 \d
描述时,psql的最新版本应该告诉您唯一索引和唯一约束之间的区别。
Recent versions of psql should tell you the difference between a unique index and a unique constraint when looking at the \d
description of a table.
这篇关于丢弃唯一约束的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!