Many-to-many local mapping using a model

I am using Slick along with a case class model for each of my tables and I want to map relationships, but I don’t know how. In the table, Surveys

I am trying to map the id to the join table and Questions

however when trying to get the list of models Question

row by row:

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

      

I am getting 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 won't work, what is the correct way to get a list of models from a connection using Slick?

+3


source to share





All Articles