我有一个带有 -simplified - 域类的 Grails 应用程序,如下所示:
class Capacity {
static constraints = {
month(blank:false, nullable:false)
company(blank:false, nullable:false)
}
Date month
Company company
String note
...
}
对月-公司必须是唯一的。 (即它们应该是数据库的主键)。
我如何定义这样的约束?
非常感谢提前
路易斯
最佳答案
由于您希望这是一个 composite primary key在数据库中,您必须声明该映射:
class Capacity implements Serializable {
Date month
Company company
String note
...
static mapping = {
id composite:['month', 'company']
}
}
产生下表(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
关于Grails 域约束定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2073210/