grails/gorm多列唯一约束冲突
问题描述:
我在下面包含了我的域。我试图在列上指定一个唯一的约束条件,使列的唯一性基于另一列的值,并且在父域中是唯一的。grails/gorm多列唯一约束冲突
public enum PostStatus {
PUBLISHED,
PENDING_REVIEW,
DRAFT,
TRASH
}
public enum PostType {
INTRO,
FUTURE,
FAMILY
}
class Post {
String content
PostType postType
PostStatus postStatus
Date dateCreated
Date lastUpdated
static belongsTo = [basicProfile:BasicProfile]
static constraints = {
content(blank:true, nullable:true, maxSize:5000)
postType(blank:false, nullable:false)
postStatus(blank:false, nullable:false, unique:'postType') //status must be unique within each postType, and within the parent.
}
static mapping = {
content sqlType:'text'
}
}
class Profile {
static hasMany = [
post:Post
]
}
现在,postStatus在postType中是唯一的,但它将唯一约束应用于Post表。所以这个表允许每个postType有一个postStatus,然后发生一个唯一的约束冲突。我需要的是每个配置文件每个postType一个独特的postStatus。
职位表中插入例如: 良好记录#1: 配置文件ID:1 post_status:DRAFT post_type:INTRO
良好记录#2: 配置文件ID:1个 post_status:公布 post_type:INTRO
良好记录#3: 配置文件ID:1 post_status:DRAFT post_type:未来
不良记录#4违反记录1的唯一约束,即使它是针对不同的配置文件ID。 文件ID:2 post_status:DRAFT post_type:INTRO
邮政通过belongsTo配置文件,所以我将如何定义约束,使其每简介独特之处?基本上我试图让在复合唯一键:
profile.id + postType + postStatus
答
你试过在http://grails.org/doc/latest/ref/Constraints/unique.html描述的最后一个例子?
e.g:postStatus(空白:假,可为空:假的,独特的:[ 'postType', 'basicProfile'])
即解决方案工作。 – spock99