Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SORM: How can I declare foreign keys?

Tags:

scala

sorm

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.

like image 523
AkelaWolf Avatar asked Nov 29 '12 15:11

AkelaWolf


1 Answers

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.

like image 168
Nikita Volkov Avatar answered Sep 19 '22 23:09

Nikita Volkov