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

发布于 2024-10-22 03:24:02 字数 1430 浏览 4 评论 0原文

我想知道是否有人对如何确保在连接表 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]

}

但出现以下错误:

原因: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍白女子 2024-10-29 03:24:02

mm_author_books 类(更好的名称是 AuthorBookRelationship)没有属性 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

    }
}

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

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文