gpt4 book ai didi

java - 如何确保对多列(mm_book_id 和 mm_author_id)应用唯一约束?

转载 作者:搜寻专家 更新时间:2023-11-01 02:54:03 24 4
gpt4 key购买 nike

我想知道是否有人对如何确保在连接表 mm_author_books 中有想法,如何确保列(mm_book_idmm_author_id ) 是独一无二的?例如,我不希望表中包含 book_idauthor_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]

}

但出现以下错误:

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

感谢和问候

是啊

最佳答案

类 mm_author_books(更好的名称是作者书籍关系)没有属性 mm_author_id 和 mm_book_id。试试这个:

class AuthorBookRelationship {

String bookAgencyName

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

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

}
}

关于java - 如何确保对多列(mm_book_id 和 mm_author_id)应用唯一约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5298537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com