SORM:如何声明外键? [英] SORM: How can I declare foreign keys?
问题描述
我对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)
如何声明外键关系UserSite.userId-> User.id?我看到了ForeignKey类,但是没有任何使用它的示例.
How can I declare foreign key relation UserSite.userId -> User.id? I see class ForeignKey, but there are no any example of using it.
谢谢.
推荐答案
一个很好的机会来展示SORM的功能.
A very nice opportunity to exhibit the powers of SORM.
如SORM的功能所述,它从 ALL 中抽象出来>关系概念.这包括外键.
As indicated in the Features of SORM, it abstracts away from ALL relational concepts. This includes foreign keys.
外键抽象是通过自然直接引用提供的,这些直接引用是要使用外键引用的这些实体.因此,您应该使用user
属性指向User
本身,而不是userId
指向User
的id
:
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.
旁注.在使用SORM时,您应该毫无限制地按照希望在Scala中使用模型的方式来设计模型,并且绝对应该把设计模型时习惯的所有关系概念都抛在脑后.这就是SORM的方式.
关于文档和库结构.该方法非常简单:如果没有记录,则不应将其用作公共API的一部分.同样,对于SORM的当前(v.0.3.x
)结构,公共API的所有组件都位于sorm._
程序包中,因此另一个规则是,如果不存在,则不打算将其用于公共API.
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.
这篇关于SORM:如何声明外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!