4

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!

Henrique Ferrolho
  • 912
  • 1
  • 10
  • 30

1 Answers1

1

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.

nemoo
  • 3,269
  • 4
  • 38
  • 51
  • Unfortunately, it did not work but I got more info on the problem! The import gets me the following error: *object AlbumRepository is not a member of package dal Note: class AlbumRepository exists, but it has no companion object.* – Henrique Ferrolho Oct 07 '15 at 19:12
  • One more question: if I skip the whole foreign key stuff it still works?? Are you saying this is useless? I thought that this mapping was the stuff responsible for making the following work afterwards: `music.album.name` - where this would hold the name of the album of the music – Henrique Ferrolho Oct 07 '15 at 19:18
  • 1
    music.album.name will still not be possible: https://groups.google.com/forum/#!msg/scalaquery/esFb2DjoHVE/NtMj7BmpE94J AFAIK it is just used for the code generator, so i dont use it at all. – nemoo Oct 07 '15 at 19:27