Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Slick's mapped tables with foreign keys?
    primarykey
    data
    text
    <p>I'm struggling with Slick's lifted embedding and mapped tables. The API feels strange to me, maybe just because it is structured in a way that's unfamiliar to me. </p> <p>I want to build a Task/Todo-List. There are two entities: </p> <ul> <li>Task: Each task has a an optional reference to the next task. That way a linked list is build. The intention is that the user can order the tasks by his priority. This order is represented by the references from task to task.</li> <li><p>TaskList: Represents a TaskList with a label and a reference to the first Task of the list.</p> <p><code>case class Task(id: Option[Long], title: String, nextTask: Option[Task])</code><br> <code>case class TaskList(label: String, firstTask: Option[Task])</code></p></li> </ul> <hr> <p>Now I tried to write a data access object (DAO) for these two entities.</p> <pre><code>import scala.slick.driver.H2Driver.simple._ import slick.lifted.MappedTypeMapper implicit val session: Session = Database.threadLocalSession val queryById = Tasks.createFinderBy( t =&gt; t.id ) def task(id: Long): Option[Task] = queryById(id).firstOption private object Tasks extends Table[Task]("TASKS") { def id = column[Long]("ID", O.PrimaryKey, O.AutoInc) def title = column[String]("TITLE") def nextTaskId = column[Option[Long]]("NEXT_TASK_ID") def nextTask = foreignKey("NEXT_TASK_FK", nextTaskId, Tasks)(_.id) def * = id ~ title ~ nextTask &lt;&gt; (Task, Task.unapply _) } private object TaskLists extends Table[TaskList]("TASKLISTS") { def label = column[String]("LABEL", O.PrimaryKey) def firstTaskId = column[Option[Long]]("FIRST_TASK_ID") def firstTask = foreignKey("FIRST_TASK_FK", firstTaskId, Tasks)(_.id) def * = label ~ firstTask &lt;&gt; (Task, Task.unapply _) } </code></pre> <p>Unfortunately it does not compile. The problems are in the <code>*</code> projection of both tables at <code>nextTask</code> respective <code>firstTask</code>. </p> <ul> <li><code>could not find implicit value for evidence parameter of type scala.slick.lifted.TypeMapper[scala.slick.lifted.ForeignKeyQuery[SlickTaskRepository.this.Tasks.type,justf0rfun.bookmark.model.Task]]</code></li> <li><code>could not find implicit value for evidence parameter of type scala.slick.lifted.TypeMapper[scala.slick.lifted.ForeignKeyQuery[SlickTaskRepository.this.Tasks.type,justf0rfun.bookmark.model.Task]]</code></li> </ul> <p>I tried to solve that with the following TypeMapper but that does not compile, too.</p> <pre><code> implicit val taskMapper = MappedTypeMapper.base[Option[Long], Option[Task]]( option =&gt; option match { case Some(id) =&gt; task(id) case _ =&gt; None }, option =&gt; option match { case Some(task) =&gt; task.id case _ =&gt; None }) </code></pre> <ul> <li><code>could not find implicit value for parameter tm: scala.slick.lifted.TypeMapper[Option[justf0rfun.bookmark.model.Task]]</code></li> <li><code>not enough arguments for method base: (implicit tm: scala.slick.lifted.TypeMapper[Option[justf0rfun.bookmark.model.Task]])scala.slick.lifted.BaseTypeMapper[Option[Long]]. Unspecified value parameter tm.</code></li> </ul> <p><strong>Main question: How to use Slick's lifted embedding and mapped tables the right way? How to I get this to work?</strong></p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload