我在使用唯一约束时遇到了问题。
允许以下组合
A.name B.name
foo NULL
foo bar
foo bar1
foo1 bar
仅当其具有不同的B时,才应该不能创建具有相同名称的新A。
通过以下约束,可以创建
A.name B.name
foo NULL
foo NULL
因为NULL似乎对唯一性没有影响。
任何提示如何解决此问题?
class A {
String name
static belongsTo = [b:B]
static constraints = {
name(unique:'b')
b(nullable:true)
}
}
class B {
String name
static hasMany = [as:A]
name(unique:true)
}
请您参考如下方法:
在数据库结构中,是否可以将列设置为NOT NULL DEFAULT 0或类似名称,然后将零与对待NULL相同,否则将为NULL?由于该列用于名称,因此值中可能总没有数字对吗?




