gpt4 book ai didi

scala - 你将如何为 MegaProtoUser 实现 self 关系?

转载 作者:行者123 更新时间:2023-12-02 05:08:54 28 4
gpt4 key购买 nike

我正在尝试为使用 Lift Mapper 框架的“ friend ”概念建模的用户类实现一个关系。我的尝试是这样的

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] ^

解决这个问题的方法是什么?

问候

最佳答案

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

如果您考虑一下,您真正​​想要的是多对多关系。

你可以这样创建:

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]

如果你想让 friend 关系是反身的,你必须做一些进一步的工作。

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

关于scala - 你将如何为 MegaProtoUser 实现 self 关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7874243/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com