我在 Play Framework 2.3 支持的 postgresql 9.4 中使用 anorm 2.4
给出一个这样的模型:
case class EmailQueue(id:UUID,
send_from:String,
send_to:String,
subject:String,
body:String,
created_date:Date,
is_sent:Boolean,
email_template:String)
这是我的解析器:
val parser: RowParser[EmailQueue] = {
get[UUID]("id") ~
get[String]("send_from") ~
get[String]("send_to") ~
get[String]("subject") ~
get[String]("body") ~
get[Date]("created_date") ~
get[Boolean]("is_sent") ~
get[String]("email_template") map {
case id ~ send_from ~ send_to ~ subject ~ body ~
created_date ~ is_sent ~ email_template=> EmailQueue(id,
send_from,
send_to,
subject,
body,
created_date,
is_sent,
email_template)
}
}
这是我的插入语句:
def insert(email:EmailQueue): Unit ={
DB.withTransaction { implicit c =>
SQL(s"""
INSERT INTO "email_queue" ( "body", "created_date", "id", "is_sent", "send_from", "send_to", "subject", "email_template")
VALUES ( {body}, {created_date}, {id}, {is_sent}, {send_from}, {send_to}, {subject}, {email_template} );
""").on(
"body" -> email.body,
"created_date" -> email.created_date,
"id" -> email.id,
"is_sent" -> email.is_sent,
"send_from" -> email.send_from,
"send_to" -> email.send_to,
"subject" -> email.subject,
"email_template" -> email.email_template
).executeInsert()
}
}
插入时我收到以下错误:
[错误] PSQLException: : 错误: 列“id”的类型为 uuid 但
表达式的类型为字符变化 [错误] 提示:您将需要
重写或转换表达式。 [错误]位置:153
(xxxxxxxxxx.java:2270)
数据库表是通过以下查询创建的:
CREATE TABLE email_queue (
id UUID PRIMARY KEY,
send_from VARCHAR(255) NOT NULL,
send_to VARCHAR(255) NOT NULL,
subject VARCHAR(2000) NOT NULL,
body text NOT NULL,
created_date timestamp without time zone DEFAULT now(),
is_sent BOOLEAN NOT NULL DEFAULT FALSE,
email_template VARCHAR(2000) NOT NULL
);