Question

Near the bottom, insertedID is an Option[Long] representing the returned id INT AUTO_INCREMENT PRIMARY KEY from MySQL. I want to populate the field in the question object before returning it to the caller.

def update(question: Question): Question = {
    DB.withConnection { implicit connection =>
        val insertedId = SQL(
          """
          replace into question values (
            {id},
            {question_text},
            {date_created},
            {subject},
            {curriculum},
            {year},
            {question_type},
            {optional_image},
            {answer_key},
            {answer_1},
            {answer_2},
            {answer_3},
            {answer_4},
            {answer_5},
            {keywords}
          )
          """
        ).on(
          'id -> question.id,
          'question_text -> question.question_text,
          'date_created -> question.date_created,
          'subject -> question.subject,
          'curriculum -> question.curriculum,
          'year -> question.year,
          'question_type -> question.question_type,
          'optional_image -> question.optional_image,
          'answer_key -> question.answer_key,
          'answer_1 -> question.answer_1,
          'answer_2 -> question.answer_2,
          'answer_3 -> question.answer_3,
          'answer_4 -> question.answer_4,
          'answer_5 -> question.answer_5,
          'keywords -> question.keywords
        ).executeInsert()

        // question.id = insertedId  Reassignment to val?

        question

    }
  }
Was it helpful?

Solution

Is Question a case class? You probably need to return this instead (as opposed to an instance of the same case class):

insertedId match {
  case Some(id) => question.copy(id = id)
  case _ => question 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top