【问题标题】:Slick compare table column with null光滑的比较表列与空
【发布时间】:2013-07-16 05:51:58
【问题描述】:

谁能告诉我如何使用 slick 的提升嵌入将表列与 NULL 进行比较。

我想在mysql中实现什么:

select * from Users where email = '<some_email_addr>' and ( removed = NULL OR removed <= 1 )

当我尝试时,它在 x.removed === null 处给我一个错误:

val q = for {
    x <- Users
        if x.email === email && ( x.removed === null || x.removed <= 1 )
} yield x.removed

谢谢

【问题讨论】:

    标签: scala slick


    【解决方案1】:

    试试:

    x.removed.isNull
    

    我想这就是你要找的东西

    【讨论】:

    • isNullisNotNull 已弃用并由 isEmptyisDefined 取代。来源:slick documentation
    【解决方案2】:

    正如 daaatz 所说,根据http://slick.typesafe.com/doc/2.1.0/upgrade.html#isnull-and-isnotnull,您应该使用isEmpty,但它仅适用于选项列。解决方法是x.removed.?.isEmpty

    【讨论】:

      【解决方案3】:

      试试

      x.removed.isEmpty()
      

      scala 中没有空值 ;-)

      【讨论】:

      • 谢谢..但我试过了,现在得到:value isEmpty is not a member of scala.slick.lifted.Column[Option[Int]]
      • .isNull 是正确的答案——scala 中可能没有空值,但 SQL 中肯定有!不过我必须承认,我也想使用普通的 Option[] 方法。如果 Slick 添加 .isEmpty / .isDefined 以使其更像 Scala,那就太好了。
      • Scala 中当然允许使用空值。它们几乎总是一个坏主意,几乎从未使用过。
      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      相关资源
      最近更新 更多