简体   繁体   中英

Grails GORM unique constraint

I've got the following ListProducts domain object;

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

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.

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?

What I want to achieve is that a product can be in many ListProducts groupings but only once per grouping.

Grails v2.2.1 against Postgres Sql database!

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

You can use unique constraint in domain class which looks like

domain1 unique:"domain2"

Both domain1 and domain2 must be a object not collection.

if you are using collection you can create another mapping class which can use unique constraint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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