SORM: How can I declare foreign keys?

送分小仙女□ 提交于 2019-12-10 13:45:01

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/13629324/sorm-how-can-i-declare-foreign-keys

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