Defining Grails Domain Limitations

I have a Grails application with the -simplified-domain class that looks like this:

class Capacity {

static constraints = {              
    month(blank:false, nullable:false)
    company(blank:false, nullable:false)
}


Date month    
Company company
String note
...
}

The month-company pair must be unique. (That is, they must be the primary key in the database).

How can I define such a restriction?

Thank you very much in advance

Louis

+5
source share
2 answers

Since you want this to be a composite primary key in the database, you will have to declare this mapping:

class Capacity implements Serializable {
    Date month    
    Company company
    String note
    ...

    static mapping = { 
        id composite:['month', 'company'] 
    } 
}

Which creates the following table (MySQL):

CREATE
    TABLE capacity
    (
        MONTH DATETIME NOT NULL,
        company_id bigint NOT NULL,
        version bigint NOT NULL,
        note VARCHAR(255) NOT NULL,
        PRIMARY KEY (MONTH, company_id),
        INDEX FKFBF514BA69595C7A (company_id)
    )
    ENGINE=MyISAM DEFAULT CHARSET=latin1
+6
source

It should be something like:

static constraints = {              
    month(blank:false, nullable:false, unique:'company')
    company(blank:false, nullable:false)
}

Take a look at http://grails.org/doc/latest/ref/Constraints/unique.html .

+5

All Articles