A B
A B

Reputation: 1936

Grails check for unique constraint

I have a Domain class Supplier:-

class Supplier {

    static embedded=['address']
    static constraints = {
        vendorName(blank:false,nullable:false,maxSize:50)
        address(blank:false,nullable:false,unique:true)
        contactPerson(blank:false,nullable:false,maxSize:50)

    }
    String vendorName
    Address address
    String contactPerson
}

and the Address class:-

class Address {
    String street
    String area

    static constraints = {
        street(blank:false,nullable:false)
        area(blank:false,nullable:false)
    }

}

My requirement is to check for the uniqueness of street in supplier. when a user enter street and area from Supplier view, i have to check that street should be unique for a vendor.

thnks

Upvotes: 0

Views: 958

Answers (2)

XenoN
XenoN

Reputation: 995

It will be like that if only street should be unique

class Address {
    String street
    String area

    static constraints = {
        street(blank:false,nullable:false)
        area(blank:false,nullable:false)
    }
    static mapping = {
        street(index: true, indexAttributes: [unique: true])
    }

}

Upvotes: 1

Kelly
Kelly

Reputation: 3709

Since you only have one Address per Supplier street is already unique per Supplier. If you can't have more than 1 address you can't have duplicate streets.

Upvotes: 0

Related Questions