Type Projection of Foreign Keys in Scala-Slick

假装没事ソ 提交于 2019-11-30 20:13:45

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!