scalaormscalikejdbc

Cannot retrieve nested object list within nested object in model with SkinnyORM


I have a model Contact with a hasMany relation to model Role. Model Role has a hasManyThrough relation with Phone with an intermediate table RolePhone. All retrieving actions are set as eager.

When I retrieve a Contact, I see its Roles list but each Role has an empty Phone list. But when I retrieve all Roles, their Phone lists contain the right Phones.

The relations are marked byDefault both in Contact and in Role, and I tried to set manually Role's joinDefinitions enabledEvenIfAssociated = true but neither of these worked

These are my models, showing only the structures and relations

case class Contact(id: Long, name: String, roles: Seq[Role] = Nil)

object Contact extends SkinnyCRUDMapper[Contact] {
  hasMany[Role](
    Role -> Role.defaultAlias,
    (contact, role) => sqls.eq(contact.id, role.contactId),
    (contact, roles) => contact.copy(roles = roles),
  ).byDefault
}

case class RoleCommand(name: String, phoneNumbers: Seq[Int])

case class Role(id: Long, name: String, contactId: Long, phones: Seq[Phone] = Nil)

object Role extends SkinnyCRUDMapper[Role] {
  hasManyThrough[RolePhone, Phone](
    through = RolePhone -> RolePhone.defaultAlias,
    throughOn = (r, rp) => sqls.eq(r.id, rp.roleId),
    many = Phone -> Phone.defaultAlias,
    on = (rp, p) => sqls.eq(rp.phoneId, p.id),
    merge = (role, phones) => role.copy(phones = phones)
  ).byDefault
}

case class RolePhone(roleId: Long, phoneId: Long)
case class Phone(id: Long, number: Int)

This are my database structure

create table contact (
  id bigint(20) not null auto_increment primary key,
  name varchar(50) not null unique
) engine=innoDB;

create table role (
  id bigint(20) not null auto_increment primary key,
  name varchar(50) not null unique,
  contact_id bigint(20) not null
) engine=innoDB;

create table phone (
  id bigint(20) not null auto_increment primary key,
  number int(20) not null
) engine=innoDB;

create table role_phone (
  role_id bigint(20) not null,
  phone_id bigint(20) not null
) engine=innoDB;

And this is a Main class added to expose the issue

object Main extends App with Connection with DatasourceConfig  {

  val contact1 = Contact.save("contact1",
    Seq(
      RoleCommand("role1", Seq(12345, 12321)),
      RoleCommand("role2", Seq(54321, 12321)),
      RoleCommand("role3", Seq(12345, 54321)),
    )
  )

  // retrieving a contact won't fill the phones number inside each role
  println(s"My Contact: ${Contact.findById(contact1)}")

  // retrieving a role will fill the phones list
  println(s"All Roles: ${Role.findAll()}")

}

Full code is at https://github.com/airabinovich/testing_skinny

I expected retrieving a Contact would get the phones list inside each role Following the example above, it should be like this

Contact(
  1,
  contact1,
  Vector(
    Role(1,role1,1,Vector(Phone(1,12345), Phone(2,12321))),
    Role(2,role2,1,Vector(Phone(3,54321), Phone(2,12321))),
    Role(3,role3,1,Vector(Phone(1,12345), Phone(3,54321)))
  )
)

But I get this

Contact(
  1,
  contact1,
  Vector(
    Role(1,role1,1,List()),
    Role(1,role1,1,List()),
    Role(2,role2,1,List()),
    Role(2,role2,1,List()),
    Role(3,role3,1,List()),
    Role(3,role3,1,List())
  )
)

Solution

  • As Kazuhiro Sera answered in his pull request, an eager loading can be done by altering a bit the relation

    object Contact extends SkinnyCRUDMapper[Contact] {
      val rolesRef = hasMany[Role](
        Role -> Role.defaultAlias,
        (contact, role) => sqls.eq(contact.id, role.contactId),
        (contact, roles) => contact.copy(roles = roles),
      ).includes[Role](
        merge = (contacts, roles) => contacts.map(c => c.copy(roles = roles.filter(_.contactId == c.id)))
       ).byDefault
    }
    

    and when retrieveing from the database include the relation

     println(s"My Contact: ${Contact.includes(Contact.rolesRef).findById(contact1)}")