我正在为每个表使用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
我有Surveys
,Questions
和SurveysQuestions
,其定义如下:
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从联接中获取模型列表的正确方法是什么?