【发布时间】:2014-05-13 04:31:30
【问题描述】:
以下是我的域类详细信息。
class Age {
String agetype
static constraints = {
}
}
我正在使用 HeidiSQL。我想删除自动生成的 id 列。并将主键设置为“agetype”。我能做什么?
【问题讨论】:
标签: grails identifier generated heidisql
以下是我的域类详细信息。
class Age {
String agetype
static constraints = {
}
}
我正在使用 HeidiSQL。我想删除自动生成的 id 列。并将主键设置为“agetype”。我能做什么?
【问题讨论】:
标签: grails identifier generated heidisql
如果不需要默认的id,则可以在映射块内轻松自定义标识符。
class Age {
String agetype
static mapping = {
id name: 'agetype',
column: 'AGE_TYPE', // if the column name is AGE_TYPE
generator: 'assigned' // Unique String should assigned for agetype
}
static constraints = {
agetype bindable: true //identifiers are not bindable by default
}
}
通过上述设置,您应该能够将 Age 创建为:
new Age(agetype: 'Teen').save(flush: true)
如果再次运行以上将通过主键冲突。
【讨论】:
AGE_TYPE,那么它不会自动推断。
DataSource.groovy 在相关环境中是否有dbCreate = "create-drop"。
mapping 而不是 mappings 作为 static mapping = { .. }。应用重启后现在应该可以工作了。