Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I cast Any returned from executeInsert in Anorm to Long
    primarykey
    data
    text
    <p>In my Scala Playframework app I'm trying to, with <code>executeInsert</code>, create a sms_token(class). Then with the primary key create a unique token, add it to sms_token and save it with <code>executeUpdate</code>. </p> <pre><code>case class SmsToken(id: Option[Long], token: String, phoneNumber: String, startDate: Option[Date], endDate: Option[Date], used: Boolean, tempReviewGrade: Option[Int], tempReviewText: Option[String]) object SmsToken { val simple = { get[Option[Long]]("id") ~ get[String]("token") ~ get[String]("phone_number") ~ get[Option[Date]]("start_date") ~ get[Option[Date]]("end_date") ~ get[Boolean]("used") ~ get[Option[Int]]("temp_review_grade") ~ get[Option[String]]("temp_review_text") map { case id ~ token ~ phone_number ~ start_date ~ end_date ~ used ~ temp_review_grade ~ temp_review_text =&gt; SmsToken(id, token, phone_number, start_date, end_date, used, temp_review_grade, temp_review_text) } } } </code></pre> <p>method: </p> <pre><code> def createToken(n: String): Option[Long] = { var addedTokenPk = 0L val result = DB.withConnection { implicit connection =&gt; SQL("insert into sms_token(token, phone_number, start_date, end_date, used, temp_review_grade, temp_review_text) values({token},{phone_number},{start_date},{end_date},{used}, {temp_review_grade}, {temp_review_text})").on( 'token -&gt; "", 'phone_number -&gt; n, 'start_date -&gt; new Date(), 'end_date -&gt; new Date(), 'used -&gt; 0, 'temp_review_grade -&gt; 0, 'temp_review_text -&gt; "" ).executeInsert() } result match { case Some(pk) =&gt; { addedTokenPk = pk.asInstanceOf[Long] } case None =&gt; println("YAAARRRRR") } if (addedTokenPk != 0L) { val token = Util.createUniqueToken(addedTokenPk) DB.withConnection { implicit connection =&gt; SQL("update sms_token s set s.token={token} where s.id={id}").on( 'id -&gt; ("" + addedTokenPk).toLong, 'token -&gt; token ).executeUpdate() } return Some(addedTokenPk) } None } </code></pre> <p>my question is regarding this part:</p> <pre><code>result match { case Some(pk) =&gt; { addedTokenPk = pk.asInstanceOf[Long] } case None =&gt; println("YAAARRRRR") } </code></pre> <p>executeInsert returns an Any object and since the key is Long I'm doing <code>asInstanceOf[Long]</code> since I need it later in the update part. I'm a Scala noob so I'm not sure that this is correct.. Maybe there is some better Scala way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload