Slick Mapping Many-To-Many with Model

282 views Asked by At

I am using Slick along with a case class Model for each of my tables, and want to map a relationship, but am not sure how. On Surveys table I try to map the id to the join table and over to the Questions, however when trying to get the list of Question models along the lines of:

def * = (id.?, name, questions.list) <> (Survey.tupled, Survey.unapply)

I get the following compilation error:

could not find implicit value for parameter session: scala.slick.jdbc.JdbcBackend#SessionDef

I have Surveys, Questions, and SurveysQuestions with the following definitions:

case class Survey(id: Option[Long], name: String) {

}

class Surveys(tag: Tag) extends Table[Survey](tag, "surveys") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def name = column[String]("name")
  def questions = SurveysQuestions.surveysQuestions.filter(_.surveyId === id).flatMap(_.questionsFk)

  def * = (id.?, name) <> (Survey.tupled, Survey.unapply)
}

case class Question(id: Option[Long], question: String) {

}

class Questions(tag: Tag) extends Table[Question](tag, "questions") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def question = column[String]("question")
  def survey = SurveysQuestions.surveysQuestions.filter(_.questionId === id).flatMap(_.surveyFk)

  def * = (id.?, question) <> (Question.tupled, Question.unapply)
}

case class SurveyQuestion(id: Option[Long], surveyId: Long, questionId: Long) {

}

class SurveysQuestions(tag: Tag) extends Table[SurveyQuestion](tag, "surveys_questions") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def surveyId = column[Long]("survey_id")
  def questionId = column[Long]("question_id")
  def surveyFk = foreignKey("survey_id", surveyId, Surveys.surveys)(_.id)
  def questionsFk = foreignKey("question_id", questionId, Questions.questions)(_.id)

  def * = (id.?, surveyId, questionId) <> (SurveyQuestion.tupled, SurveyQuestion.unapply)
}

Given that this doesn't work, what is the proper way of getting a list of models out of a join using Slick?

0

There are 0 answers