【问题标题】:Type Mistmatch Scala Slick Query类型不匹配 Scala Slick 查询
【发布时间】:2021-03-20 08:11:25
【问题描述】:

我在使用 scala 中的 slick 获取用户名和密码时遇到问题,基本上类似于

var query = "SELECT * FROM  \"default\".users as A " + "WHERE " + " A.username LIKE \'" + email + "\' " + "AND" + " A.password LIKE \'" + password + "\' ";

这是我的架构案例类

  case class User(
                       id: Long = 0L,
                       username: String,
                       password: String,

                       author_id:Long,
                       created_on: DateTime,
                       updated_by: Long,
                       updated_on:Option[DateTime]
                     )

  class UserTable(tag:Tag) extends Table[User](tag,"user"){
    override def * = (id,username,password,author_id,created_on,updated_by,updated_on.?) <> (User.tupled,User.unapply)

    def id = column[Long]("id",O.PrimaryKey,O.AutoInc)

    def username = column[String]("username")

    def password = column[String]("password")

    def created_on = column[DateTime]("created_on")

    def updated_on = column[DateTime]("dateupdated")

    def author_id = column[Long]("author_id")

    def updated_by = column[Long]("updated_by")
  }

 lazy  val UserTable = TableQuery[UserTable]

以下是我的查询

val users = Main.UserTable.filter(_.username == email)
      .filter(_.password  == password).take(1)

【问题讨论】:

  • 我得到一个类型不匹配异常
  • 请显示实际的完整错误信息

标签: database postgresql scala slick


【解决方案1】:
filter(_.username == email)

您可能指的是===,而不是==

这是构建查询表达式的 Slick DSL 的一部分(他们不能称之为 ==,因为 Scala 的相等比较器无法替换)。

Warning:大多数运算符都模仿普通的 Scala 等效项,但是您必须使用 === 而不是 == 来比较两个值是否相等,使用 =!= 而不是 != 来表示不相等。这是必要的,因为这些运算符已经在基本类型 Any 上定义(具有不合适的类型和语义),因此它们不能被扩展方法替换。

【讨论】:

    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 2012-08-27
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多