2013-10-02 3 views
1

아래 도메인을 포함 시켰습니다. 다른 열의 값을 기반으로 고유 한 열을 만들고 부모 도메인 내에서 고유하게하는 열에 고유 한 제약 조건을 지정하려고합니다.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 : 1 post_status : DRAFT post_type : INTRO

좋은 기록 # 2 : 프로파일 ID : 1 post_status : 게시 된 post_type : INTRO ID 프로필

후 나타난 예 삽입

좋은 기록 # 3 : 프로필 ID : 1 post_status : 초안 post_type : 미래

나쁜 레코드 # 4는 다른 프로필 ID에도 불구하고 레코드 1의 고유 제약 조건을 위반합니다. 프로필 ID : 2 post_status : 초안 post_type : INTRO

포스트 belongTo 프로파일, 그래서 어떻게 프로필 당이 독특한 만들 수있는 제약 조건을 정의 할 것인가? 기본적으로 나는 화합물 고유 키에서 얻으려고 :

profile.id + postType postStatus

답변

관련 문제