【发布时间】:2013-05-11 13:37:14
【问题描述】:
我有一个给定的 postgres 数据库,它使用具有以下值“M”、“F”的枚举类型。该值可以为空。当我尝试保存域对象时,我收到以下错误。
postgres 枚举:
CREATE TYPE genderlist AS ENUM ('M','F')
我的枚举:
public enum GenderCode {
M,F
private final String value
private static list
public String value() {
return name()
}
public static GenderCode fromValue(String v) {
return valueOf(v)
}
}
域类:
class UserLog {
String gender = null //default null
..
}
BootStrap.groovy:
// reading - works fine now
def rows = UserLog.list()
// insert - does not work
UserLog u = new UserLog(gender:null)
u.save()
错误:
| Batch-Entry 0 insert into user_log (active, birthyear, country, gender, user_id, id) values ('1', NULL, NULL, NULL, NULL, 98304) was cancelled.
| ERROR: column "gender" is of type genderlist but expression is of type character varying
Note: You will need to rewrite or cast the expression.
编辑
我更新了整个问题以匹配我项目的当前状态。由于我更新了枚举,我不再有读取数据的问题。 PGObject 被正确地转换为字符串表示。尝试插入数据时,转换仍然是一个问题。
我有一个肮脏的小变通办法:
def sql = new Sql(dataSource_log)
def map = [gender:'M']
sql.executeInsert "insert into user_log (gender) values ('${map.gender}')"
我的问题仍然没有改变:我如何正确地转换字符串插入时? 'null' 值还没有被接受。 谢谢!
【问题讨论】:
标签: grails enums grails-orm