Note that there are some explanatory texts on larger screens.

plurals
  1. POType Projection of Foreign Keys in Scala-Slick
    primarykey
    data
    text
    <p>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 projections. In the example at page bottom it currently doesn't compile because a ForeignKey cannot be lifted directly, so what's the correct way to have query results lift into my type (which is basically this sans methods and additional constructors):</p> <pre><code>case class UserCompanyPermission(pk: UUID, company: Company, user: User, accessLevel: CompanyPermissionLevel) </code></pre> <p>Either I'd like to have the type projection return a UserCompanyPermission, otherwise have a way to wrap everything coming into and out of the DAO methods so from the outside I'm just passing my Scala Types in. Basically I want to ensure my business logic is entirely decoupled from state to make it easier to test, so being able to keep all table specifics restricted to this storage package. Is their way to wrap, or is each method I write in the DAO object going to need to make the conversion on its own?</p> <pre><code>trait CompaniesComponent { this: UsersComponent =&gt; val Companies: Companies val UserCompanyPermissions: UserCompanyPermissions implicit val companyPermissionLevelTypeMapper = MappedTypeMapper.base[CompanyPermissionLevel.Value, Int]( { level =&gt; level.id }, { id =&gt; CompanyPermissionLevel(id) } ) class Companies extends Table[Company]("Company") { def pk = column[UUID]("pk", O.PrimaryKey) def subdomain = column[String]("subdomain", O.NotNull) def name = column[String]("name", O.NotNull) def * = pk ~ subdomain ~ name &lt;&gt; (Company.apply _, Company.unapply _) } class UserCompanyPermissions extends Table[UserCompanyPermission]("UserCompanyPermission") { def pk = column[UUID]("pk", O.PrimaryKey) def company_pk = column[UUID]("company_pk", O.NotNull) def user_pk = column[UUID]("user_pk", O.NotNull) def accessLevel = column[CompanyPermissionLevel.Value]("access_level", O.NotNull) def company = foreignKey("company_pk", company_pk, Companies)(_.pk) def user = foreignKey("user_pk", user_pk, Users)(_.pk) def * = pk ~ company ~ user ~ accessLevel &lt;&gt; (UserCompanyPermission.apply _, UserCompanyPermission.unapply _) } } object Companies extends DAO { def insert(company: Company)(implicit session: Session) { Companies.insert(company) } } object UserCompanyPermissions extends DAO { def insert(perm: UserCompanyPermission)(implicit session: Session) { UserCompanyPermissions.insert(perm) } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload