How to properly use Scala Play Anorm and Option[String] to insert NULL SQL

…衆ロ難τιáo~ 提交于 2019-12-07 12:51:49

问题


What is the proper way to insert Option[String] when it is None? The below code inserts an empty string, which is not the same as NULL in mysql.

Is the only way to build the SQL string beforehand based on the content of partnerCode? Sigh...Anorm...

DB.withConnection { implicit connection =>

  val id: Option[Long] = SQL(
    """
    INSERT INTO users (email, partner_code, is_active, created, pass) VALUES ({email}, {partnerCode}, 0, NOW(), {pass})
    """
  ).on(
    'email -> user.email,
    'partnerCode -> user.partnerCode.getOrElse(""),  // FIXME: how to use NULL keyword instead of empty string? Is Anorm just this dumb?
    'pass -> hashPassword(user.password.get)
  ).executeInsert()

  id.get

}

回答1:


None should actually work fine if you import anorm._ or anorm.toParameterValue:

'partnerCode -> user.partnerCode


来源:https://stackoverflow.com/questions/22520254/how-to-properly-use-scala-play-anorm-and-optionstring-to-insert-null-sql

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