使用模型进行多对多的光滑映射

时间:2014-11-17 03:01:55

标签: scala slick-2.0

我正在为每个表使用Slick和一个案例类模型,并希望映射一个关系,但我不确定如何。在Surveys表格中,我尝试将ID映射到连接表,然后映射到Questions,但是当尝试获取Question模型列表时,我会尝试:

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

我收到以下编译错误:

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

我有SurveysQuestionsSurveysQuestions,其定义如下:

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)
}

鉴于这不起作用,使用Slick从联接中获取模型列表的正确方法是什么?

0 个答案:

没有答案