你如何为MegaProtoUser实现自我关系?

时间:2011-10-24 10:25:27

标签: scala orm lift

我正在尝试使用Lift Mapper框架为“朋友”概念建模用户类。我的尝试就是这样的

object User extends User with MetaMegaProtoUser[User] {
    override def dbTableName = "users" // define the DB table name
    override def screenWrap = Full(<lift:surround with="default" at="content">
               <lift:bind /></lift:surround>)
    // define the order fields will appear in forms and output
    override def fieldOrder = List(id, firstName, lastName, email,
                          locale, timezone, password)

    // comment this line out to require email validations
    override def skipEmailValidation = true
 }

class User extends MegaProtoUser[User] with OneToMany[Long,User]
{
   def getSingleton = User // what's the "meta" server
   object friends extends MappedOneToMany(User, User.id)
}

但由于类型不匹配而无法编译

[error]  src/main/scala/code/model/User.scala:30: type mismatch;
[error]  found   : net.liftweb.mapper.MappedLongIndex[code.model.User]
[error]  required: net.liftweb.mapper.MappedForeignKey[Long,?,code.model.User]
[error]   object friends extends MappedOneToMany(User, User.id)
[error]                                                     ^

这是怎么回事?

此致

1 个答案:

答案 0 :(得分:0)

如果您查看错误消息,您会看到OneToMany帮助程序在关系的“很多”端需要一个外部键,指向此“one”。 User.id是主键,因此不适合。

如果你想到它,你想要的实际上是一个ManyToMany关系。

您可以这样创建:

object User extends User with MetaMegaProtoUser[User] with MappedManyToMany {

...

object friends extends MappedManyToMany(Friends, Friends.friend, Friends.friendee, User)

你有一个联接表:

class Friends extends Mapper[Friends] {
  object friend extends MappedLongForeignKey(this, User) {
    override def dbIndexed_? = true
  }
  object friendee extends MappedLongForeignKey(this, User) {
    override def dbIndexed_? = true
  }
  def getSingleton = Friends
}
object Friends extends Friends with MetaMapper[Friends]

如果你希望朋友关系具有反身性,你就必须做进一步的工作。

我猜这是一个非常晚的答案。我建议发送到电梯邮件列表,以获得极快的回复:https://groups.google.com/forum/?fromgroups#!forum/liftweb