我在下面包含了我的域。我試圖在列上指定一個唯一的約束條件,使列的唯一性基於另一列的值,並且在父域中是唯一的。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
即解決方案工作。 – spock99