Search code examples
scalaplayframeworkslickslick-3.0

Scala Slick extend the functionality of a table outside of its structure


I'm using Slick code-gen to output a very normal Tables.scala file, which maps the tables/columns of the structure of my database.

However i want to EXTEND the functionality of those tables in my DAOs and its proving to be impossible for me (roughly new to scala and play framework)

INSIDE Tables.scala INSIDE the class Meeting you can write functions that have access to the columns I.E

  class Meeting(_tableTag: Tag) extends profile.api.Table[MeetingRow](_tableTag, "meeting") {
def * = (id, dateandtime, endtime, organisationid, details, adminid, datecreated, title, agenda, meetingroom) <> (MeetingRow.tupled, MeetingRow.unapply)
def ? = (Rep.Some(id), Rep.Some(dateandtime), Rep.Some(endtime), Rep.Some(organisationid), Rep.Some(details), Rep.Some(adminid), Rep.Some(datecreated), Rep.Some(title), Rep.Some(agenda), Rep.Some(meetingroom)).shaped.<>({r=>import r._; _1.map(_=> MeetingRow.tupled((_1.get, _2.get, _3.get, _4.get, _5.get, _6.get, _7.get, _8.get, _9.get, _10.get)))}, (_:Any) =>  throw new Exception("Inserting into ? projection not supported."))
val id: Rep[Int] = column[Int]("id", O.AutoInc, O.PrimaryKey)
val dateandtime: Rep[java.sql.Timestamp] = column[java.sql.Timestamp]("dateandtime")
val endtime: Rep[java.sql.Timestamp] = column[java.sql.Timestamp]("endtime")
val organisationid: Rep[Int] = column[Int]("organisationid")
val details: Rep[String] = column[String]("details", O.Default(""))
val adminid: Rep[Int] = column[Int]("adminid")
val datecreated: Rep[java.sql.Timestamp] = column[java.sql.Timestamp]("datecreated")
val title: Rep[String] = column[String]("title", O.Default(""))
val agenda: Rep[String] = column[String]("agenda", O.Default(""))
val meetingroom: Rep[Int] = column[Int]("meetingroom")

def getAttendees = Tables.Meeting2uzer.filter(_.meetingid === id)

where "ID" in the above function is a column in Meeting.

now the problem arises when i want to write that same function "getAttendees" in my DAO which doesn't have access to the columns in scope.

something along the lines of....

@Singleton
class SlickMeetingDAO @Inject()(db: Database)(implicit ec: ExecutionContext) extends MeetingDAO with Tables {

    override val profile: JdbcProfile = _root_.slick.jdbc.PostgresProfile
    import profile.api._

    private val queryById = Compiled((id: Rep[Int]) => Meeting.filter(_.id === id))

    def getAttendees = Meeting2uzer.filter(_.meetingid === "NEED ID OF COLUMN IN SCOPE")

How do i get 'id' which is a column in Tables.Meeting in scope in my DAO to finish this getAttendees function.


Solution

  • If I understand correctly, you're trying to join two tables?

    Meeting2uzer.join(Meeting).on(_.meetingid === _.id)

    What you've done inside the Tables.scala, would be more inline with creating a foreign key in Slick. You could create a slick foreign key, instead of using an explicit join. See the documentation for Slick here.