简体   繁体   English

Grails GORM唯一约束

[英]Grails GORM unique constraint

I've got the following ListProducts domain object; 我有以下ListProducts域对象;

String name
Date lastUpdated
Date dateCreated = new Date()
String lastUpdatedBy
CatLists catLists // 
Product product // has a composite primary key
ListProducts listProducts

static constraints = {
    name nullable: true, blank: true
    catLists nullable: false
    lastUpdatedBy nullable: true
    lastUpdated nullable: true
    dateCreated nullable: false
    product unique: 'listProducts'
}

static mapping = {
    version true
    table 'list_products'
    sort "name"
}

The Product domain class has the following constraint which is a composite key Product域类具有以下约束,它是一个组合键

FOREIGN KEY (product_fulfillment_sku, product_merchant_id) REFERENCES product(fulfillment_sku, merchant_id)

as it doest not have a primary key ie ID as the table is dropped due to a batch job so the unique values are the sku and the merchant id. 因为它没有主键,即由于批处理作业而导致表被删除,所以没有ID,因此唯一值是sku和商户ID。

I can see in the list_products table the product_fulfillment_sku, product_merchant_id and cat_lists_id columns, but how do I represent this in grails domain classes so that product cant be added if it already exists? 我可以在list_products表中看到product_fulfillment_sku,product_merchant_id和cat_lists_id列,但是如何在grails域类中表示此名称,以便不能添加已经存在的产品?

What I want to achieve is that a product can be in many ListProducts groupings but only once per grouping. 我要实现的是,一个产品可以在许多ListProducts分组中,但每个分组只能一次。

Grails v2.2.1 against Postgres Sql database! 针对Postgres Sql数据库的Grails v2.2.1!

Any ideas? 有任何想法吗?

J Ĵ

So the work around I got was to remove the dependency on the unique constraint. 因此,我要解决的问题是消除对唯一约束的依赖。 This would also block parrallel lookups as the table is huge. 由于表很大,因此这也将阻止并行查找。 Took some inspiration from Performance Collections Performance Collections获得一些灵感

You can use unique constraint in domain class which looks like 您可以在域类中使用唯一的约束,如下所示

domain1 unique:"domain2"

Both domain1 and domain2 must be a object not collection. domain1和domain2都必须是对象而不是集合。

if you are using collection you can create another mapping class which can use unique constraint. 如果使用集合,则可以创建另一个可以使用唯一约束的映射类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM