开发者

How to ensure unique constraint is applied on multiple columns (mm_book_id and mm_author_id )?

I would like to know if anyone has ideas on how to ensure if in the join table mm_author_books, how to make sure the columns (mm_book_id and mm_author_id) are unique? For instance, I don't want the table to contain duplicate records of book_id and author_id like 1,1 and 1,1. So how to do this...

class Book {
    String title 
    static belongsTo = Author 
    static hasMany = [authors:Author]
    static mapping = { 
        authors joinTable:[name:"mm_author_books", key:'mm_book_id' ] 
    }
}

class Author { 
    String name
    static hasMany = [books:Book]
    static mapping = { 
        books joinTable:[name:"mm_author_books", key:'mm_author_id'] 
    }
}

I have tried this in "mm_author_books" domain:

class mm_author_books {

 String book_agency_name    
 static constraints = {
    book_agency_name(unique:['mm_author_id','mm_book_id'])
    }
static belongsTo = [authors:Author, books:Book]

}

but getting the following error:

Caused by: org.codehaus.groovy.grails.validation.excepti开发者_运维知识库ons.ConstraintException: Exception thrown applying constraint [unique] to class [class content_hub_admin.mm_author_books] for value [[mm_author_id, mm_book_id]]: Scope for constraint [unique] of property [name] of class [class content_hub_admin.mm_author_books] must be a valid property name of same class at content_hub_admin.mm_author_books$_clinit_closure1.doCall(mm_author_books.groovy:6) at content_hub_admin.mm_author_books$_clinit_closure1.doCall(mm_author_books.groovy) ... 28 more

Thanks & Regards

rsheyeah


the class mm_author_books (better name is AuthorBookRelationship) has no atributes mm_author_id and mm_book_id. Try this:

class AuthorBookRelationship {

    String bookAgencyName

    static constraints = {
        bookAgencyName(unique:['author','book'])
    }

    static belongsTo = [author:Author, book:Book] //only one author and one book

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜