ORM UniqueConstraint,空值重复 [英] ORM UniqueConstraint, null value duplicate
问题描述
我有一个实体,对3个领域有唯一的约束.
I have an entity with an unique constraint on 3 fiels.
2个字段不可为空,第三个字段可为空.
2 fields are not nullable, and the third is nullable.
我的问题,当第三个字段为空时,唯一约束不起作用,因此我可以在数据库中包含一些重复的值,例如(5,1,null)(5,1,null)
My problem, when the third field is null, the unique constraint doesn't work, so i can have in my database some duplicate values like (5,1,null)(5,1,null)
对我来说,(5,1,null)
是一个唯一值,也为null.
For me, (5,1,null)
is an unique value, with null too.
您有什么想法吗?
这是我的ORM行:
* @ORM\Table(name="table", uniqueConstraints={@ORM\UniqueConstraint(name="table_idx", columns={"field1", "field2", "field3"})})
Edit:可为空的值是另一个实体的外键,因此我不能放置自定义值.它是一个实体或为空.
Edit : The nullable value is a foreign key to an other entity, so i can't put a custom value. It's an entity or null.
推荐答案
Am也面临同样的问题,找不到解决方法.
Am also faced the same issue, didn't find any way to resolve it.
最后在我的表中又添加了一个varchar列,它的值是3个字段的组合.例如:5_1_0(手动检查并为空约束添加0,否则为约束ID),并在新列上添加了唯一约束,并删除了现有约束.
Finally added one more varchar column to my table, its value is combination of 3 fields. ex:5_1_0(manually checking and adding 0 for null constraint otherwise constraint Id) and added unique constraint on new column and removed existing constraint.
这篇关于ORM UniqueConstraint,空值重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!