SORM: How can I declare foreign keys?
Asked Answered
M

1

5

I'm very interested in SORM, but when I try to use it I bump into problem. Suppose I have two entities:

case class User(login: String, firstName: String, lastName: String)
case class UserSite(userId: Int, name: String, url: String)

How can I declare foreign key relation UserSite.userId -> User.id? I see class ForeignKey, but there are no any example of using it.

Thank you.

Mithridatism answered 29/11, 2012 at 15:41 Comment(0)
H
10

A very nice opportunity to exhibit the powers of SORM.

As indicated in the Features of SORM, it abstracts away from ALL relational concepts. This includes foreign keys.

The foreign key abstraction is provided by natural direct references to these entities you wanted to refer to with a foreign key. So instead of userId pointing to the id of User, you should point to the User itself with the user property:

case class User(login: String, firstName: String, lastName: String)
case class UserSite(user: User, name: String, url: String)

Under the hood this will translate exactly into what you wanted to achieve with the Foreign Key. But the thing is you don't have to care about it.

A sidenote. When working with SORM you should design your model the way you want to use it in Scala with almost no limitations, and you should definitely throw all the relational concepts you got used to when designing models out of your mind. That's the way of SORM.

Concerning the documentation and library structure. The approach is very simple: if it's not documented, it is not intended to be used as part of public API. Also with the current (v. 0.3.x) structure of SORM all components of the public API reside in the sorm._ package, so another rule is if it's not there it is not intended for public API.

Houston answered 29/11, 2012 at 16:21 Comment(1)
Thank you very much! Your answer is very detailed and helpfulMithridatism

© 2022 - 2024 — McMap. All rights reserved.