我有一个带有-simplified - 域类的Grails应用程序,如下所示:
class Capacity {
static constraints = {
month(blank:false, nullable:false)
company(blank:false, nullable:false)
}
Date month
Company company
String note
...
}
Run Code Online (Sandbox Code Playgroud)
这对月份公司必须是独一无二的.(即它们应该是数据库的主键).
我该如何定义这样的约束?
非常感谢提前
路易斯
由于您希望将其作为数据库中的复合主键,因此您必须声明该映射:
class Capacity implements Serializable {
Date month
Company company
String note
...
static mapping = {
id composite:['month', 'company']
}
}
Run Code Online (Sandbox Code Playgroud)
生成下表(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
Run Code Online (Sandbox Code Playgroud)
它应该是这样的:
static constraints = {
month(blank:false, nullable:false, unique:'company')
company(blank:false, nullable:false)
}
Run Code Online (Sandbox Code Playgroud)
请查看http://grails.org/doc/latest/ref/Constraints/unique.html.
| 归档时间: |
|
| 查看次数: |
3707 次 |
| 最近记录: |