Type Projection of Foreign Keys in Scala-Slick

前端 未结 1 918
误落风尘
误落风尘 2021-01-05 07:19

I\'m using Scala, and new to Play and Slick. I\'m starting to block out a simple database structure and I\'m not sure about the correct way to handle foreign keys and projec

相关标签:
1条回答
  • 2021-01-05 07:58

    The recommended way to work with Slick is to never nest the case classes holding the values of your rows. They should only contain the actual columns, not any related objects, because that would hard-code that they have to be loaded together (unless you do some magical lazy loading under the hood which makes things complex for use and implementation). Instead you write queries, that associate the values for the particular data you need right now using tuples.

    // FYI
    case class UserCompanyPermission( pk: UUID, company_pk: UUID, user_pk: UUID, accessLevel: CompanyPermissionLevel.Value )
    
    // associating data in an ad-hoc, not hard-coded way
    for( ucp <- Query(UserCompanyPermissions).filter(_.accessLevel === LevelOne);
         c <- ucp.company;
         u <- ucp.user
    ) yield (u,c)
    

    Here we load u and c because we say so. We could have only loaded u or c or c and ucp or whatever. It is not hardcoded in our row classes.

    Architecture-wise, you will find help in our Scala Days 2013 talk and the Scala Exchange 2013 talk. http://slick.typesafe.com/docs/

    As a side node I would recommend a sealed trait with case object children instead of Enumerationfor CompanyPermissionLevel.

    0 讨论(0)
提交回复
热议问题