Describing optional fields in Slick

落爺英雄遲暮 提交于 2019-12-07 10:43:32

问题


The Slick DSL allows two ways to create optional fields in tables.

For this case class:

case class User(id: Option[Long] = None, fname: String, lname: String)

You can create a table mapping in one of the following ways:

object Users extends Table[User]("USERS") {
    def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
    def fname = column[String]("FNAME")
    def lname = column[String]("LNAME")
    def * = id.? ~ fname ~ lname <> (User, User.unapply _)
  }

and

  object Users extends Table[User]("USERS") {
    def id = column[Option[Long]]("id", O.PrimaryKey, O.AutoInc)
    def fname = column[String]("FNAME")
    def lname = column[String]("LNAME")
    def * = id ~ fname ~ lname <> (User, User.unapply _)
  }
}

What is the difference between the two? Is the one the old way and the other the new way, or do they serve different purposes?

I prefer the second choice where you define the identity as optional as part of the id definition because it's more consistent.


回答1:


The .? operator in the first one allows you to defer the choice of having your field be optional to the moment of defining your projections. Sometimes that's not what you want, but defining your PK to be an Option is perhaps a bit funny because one might expect a PK to be NOT NULL.

You can use .? in additional projections besides *, for example:

def partial = id.? ~ fname

Then you could do Users.partial.insert(None, "Jacobus") and not worry about fields you're not interested in.



来源:https://stackoverflow.com/questions/13357067/describing-optional-fields-in-slick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!