我将 Play 2.1.0 与 anorm、Scala 2.10 和 PostgreSQL(9.1,驱动程序:9.1-901.jdbc4)一起使用。以下查询在 MySQL 中运行良好。迁移到 PostgreSQL 后就没有了。进入该方法后,在“隐式连接”行中抛出“异常”,调试器直接跳转到 Sql.resultSetToStream 527 行,其中列的元数据显然已确定。在播放日志中没有显示错误...
id 字段在 MySql 中是一个整数,而在 PostgreSQL 中它是一个序列。Anorm 是否有串行列的问题?
def getUserId(userName: String): Int = {
DB.withConnection {
implicit connection =>
try {
val result = SQL("select id from users where user_name = {userName}")
.on('userName -> userName).apply().head
result[Int]("id")
} catch {
case e: SQLException =>
Logger.error(e.getStackTraceString)
//error logged, but no problem when we return 0
0
}
}
}
我在同一个表中的插入语句遇到了同样的问题。
有趣的是,以下查询有效:
def checkCredentials(userName: String, password: String): Boolean = {
DB.withConnection {
implicit connection =>
try {
val result = SQL("select count(*) as c from users where user_name = {userName} and password = crypt({password}, password)")
.on('userName -> userName,
'password -> password).apply().head
result[Long]("c") > 0
} catch {
case e: SQLException =>
Logger.error(e.getStackTraceString)
false
}
}