如何确保对多个列( mm_book_id 和 mm_author_id )应用唯一约束?
我想知道是否有人对如何确保在连接表 mm_author_books
中,如何确保列 (mm_book_id
和 mm_author_id
>) 是独一无二的吗? 例如,我不希望表包含 book_id
和 author_id
的重复记录,例如 1,1 和 1,1。那么如何做到这一点...
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']
}
}
我已经在“mm_author_books”域中尝试过此操作:
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]
}
但出现以下错误:
原因: org.codehaus.groovy.grails.validation.exceptions.ConstraintException: 应用约束抛出异常 [独特]到类[类 content_hub_admin.mm_author_books] 的 值[[mm_author_id,mm_book_id]]: 约束范围 [唯一] of 类 [class] 的属性 [名称] content_hub_admin.mm_author_books] 必须是相同的有效属性名称 上课于 content_hub_admin.mm_author_books$_clinit_closure1.doCall(mm_author_books.groovy:6) 在 content_hub_admin.mm_author_books$_clinit_closure1.doCall(mm_author_books.groovy) ... 28 更多
感谢&问候
谢耶亚
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.exceptions.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mm_author_books 类(更好的名称是 AuthorBookRelationship)没有属性 mm_author_id 和 mm_book_id。试试这个:
the class mm_author_books (better name is AuthorBookRelationship) has no atributes mm_author_id and mm_book_id. Try this: