database - How to use Slick's mapped tables with foreign keys? -
i'm struggling slick's lifted embedding , mapped tables. api feels strange me, maybe because structured in way that's unfamiliar me.
i want build task/todo-list. there 2 entities:
- task: each task has optional reference next task. way linked list build. intention user can order tasks priority. order represented references task task.
tasklist: represents tasklist label , reference first task of list.
case class task(id: option[long], title: string, nexttask: option[task])
case class tasklist(label: string, firsttask: option[task])
now tried write data access object (dao) these 2 entities.
import scala.slick.driver.h2driver.simple._ import slick.lifted.mappedtypemapper implicit val session: session = database.threadlocalsession val querybyid = tasks.createfinderby( t => 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 <> (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 <> (task, task.unapply _) }
unfortunately not compile. problems in *
projection of both tables @ nexttask
respective firsttask
.
could not find implicit value evidence parameter of type scala.slick.lifted.typemapper[scala.slick.lifted.foreignkeyquery[slicktaskrepository.this.tasks.type,justf0rfun.bookmark.model.task]]
could not find implicit value evidence parameter of type scala.slick.lifted.typemapper[scala.slick.lifted.foreignkeyquery[slicktaskrepository.this.tasks.type,justf0rfun.bookmark.model.task]]
i tried solve following typemapper not compile, too.
implicit val taskmapper = mappedtypemapper.base[option[long], option[task]]( option => option match { case some(id) => task(id) case _ => none }, option => option match { case some(task) => task.id case _ => none })
could not find implicit value parameter tm: scala.slick.lifted.typemapper[option[justf0rfun.bookmark.model.task]]
not enough arguments method base: (implicit tm: scala.slick.lifted.typemapper[option[justf0rfun.bookmark.model.task]])scala.slick.lifted.basetypemapper[option[long]]. unspecified value parameter tm.
main question: how use slick's lifted embedding , mapped tables right way? how work?
thanks in advance.
the short answer is: use ids instead of object references , use slick queries dereference ids. can put queries methods re-use.
that make case classes this:
case class task(id: option[long], title: string, nexttaskid: option[long]) case class tasklist(label: string, firsttaskid: option[long])
i'll publish article topic @ point , link here.
Comments
Post a Comment