4
votes

I am trying to map a relationship between musics and albums - a music belongs to an album, whereas one album may have many musics.

According to Slick's docs this should be fairly simple... However, I do not have my tables in a single file like in the docs, and this is where my problem arises: I do not know how I can access the albums table val correctly from MusicRepository.scala when I need to map the foreign key.

Here is the error message I am getting: not found: value AlbumRepository

I have tried to import dal.AlbumRepository but it did not work...
Does it also have to do with my val albums being private - or is that a whole other problem?

AlbumRepository.scala

package dal

/* Omitted for brevity */

@Singleton
class AlbumRepository @Inject()(dbConfigProvider: DatabaseConfigProvider)
                               (implicit ec: ExecutionContext) {

  private val dbConfig = dbConfigProvider.get[JdbcProfile]

  import dbConfig._
  import driver.api._

  private val albums = TableQuery[Albums]

  /* Omitted for brevity */

  // Albums table
  private class Albums(tag: Tag) extends Table[Album](tag, "albums") {
    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

    def name = column[String]("name")

    def description = column[String]("description")

    def * = (id, name, description) <>((Album.apply _).tupled, Album.unapply)
  }

}

MusicRepository.scala

package dal

import javax.inject.Inject

import models.Music
import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile

import scala.concurrent.{Future, ExecutionContext}

class MusicRepository @Inject()(dbConfigProvider: DatabaseConfigProvider)
                               (implicit ec: ExecutionContext) {

  /* Omitted for brevity */

  // Musics table
  private class Musics(tag: Tag) extends Table[Music](tag, "musics") {
    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

    def albumId = column[Long]("album_id")

    def title = column[String]("title")

    def lyrics = column[String]("lyrics")

    def year = column[Int]("year")

    def * = (id, albumId, title, lyrics, year) <>((Music.apply _).tupled, Music.unapply)

    //
    // THIS IS WHERE I AM GETTING THE ERROR MESSAGE
    //
    def album = foreignKey("album_fk", albumId, AlbumRepository.albums)(_.id)
  }

}

Thank you in advance!

1

1 Answers

1
votes

I think you need to import dal.AlbumRepository.Albums and yes, you probably need to make it public. Otherwise you can also skip the whole foreign key stuff in slick. Works great without it.