【问题标题】:Play, Anorm and PostgreSQL: Problems with serial columns?Play、Anorm 和 PostgreSQL:串行列的问题?
【发布时间】:2013-05-23 21:41:33
【问题描述】:

我将 Play 2.1.0 与 anorm、Scala 2.10 和 PostgreSQL(9.1,驱动程序:9.1-901.jdbc4)一起使用。以下查询在 MySQL 中运行良好。迁移到 PostgreSQL 后就没有了。进入方法后,在“隐式连接”行中抛出了一个“异常”,i.d.调试器直接跳转到 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
    }
}

【问题讨论】:

  • 连续剧不是Long类型吗?
  • 不,我曾经读过它是一个 int。但是当 sql 实际上不会返回任何行时,就会出现问题。但我不明白为什么“无行”结果在 MySQL 中没有问题,但在 PostgreSQL 中是一个。

标签: scala playframework playframework-2.0 postgresql-9.1 anorm


【解决方案1】:

问题在于 id 列。显然 PostgreSQL 认为这是一个功能词,所以如果你把它放在''之间就可以了,这意味着写 'id' 可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多