Search code examples
scalasqueryl

"not found: value account" While using squeryl and Scala


I'm trying to make it so I can get a value from one model into the creation of another model. This is very simplified but gives the error I'm having with a model instance extending a squeryl model. In reality I'm doing some calculations specific to this model, but need a value from another model.

I'm getting an error doing all sorts of versions of this:

class Bar(
  val bazId: String
  val someValue: String
) extends db.BaseModel {
  val foo: Foo = db.MyDb.foos.lookup(bazId).head

  def this(
    bazId: Baz
  ) = {
    this (
      Baz.id,
      foo.someValue,
    )
  }

The error is:

not found: value foo
[error]       foo.someValue,
              ^

I've tried having some kind of lookup in the Object/singleton, inside of the def this() (that gives a "Application does not take parameters" error), and other things but nothing is working. Seems so simple, but having no luck at all.


Solution

  • The issue is you are trying to access a member of the class in the constructor, and since this is before the object has been constructed - it is not accessible. Doing it in the companion should help you get around that. Something like:

    object Bar {
      val foo(bazId:String): Foo = db.MyDb.foos.lookup(bazId).head
    }
    
    class Bar(
      val bazId: String,
      val someValue: String
    ) extends db.BaseModel {
      val foo: Foo = Bar.foo(bazId)
    
      def this(
        bazId: String
      ) = {
        this (
          bazId,
          Bar.foo(bazId).someValue
        )
      }
    }
    

    Or, if you did intend to have the constructor accept the class Baz, then you could also add the lookup method directly in Baz - then you can just call it directly to look up Foo.