gpt4 book ai didi

scala - 对于 scalikejdbc 如何编写带有外键约束的 QueryDSL

转载 作者:行者123 更新时间:2023-12-02 01:48:21 24 4
gpt4 key购买 nike

我使用 scalikejdbc 2.0.1 和 playframework 2.3。我按照一对多 http://scalikejdbc.org/documentation/one-to-x.html 的说明进行操作, 但仍然有错误:

我的数据模型是,一个账户有很多待办事项:

待办模型:

case class Todo (id: Long, value: String, userId:Option[Long] = None, users: Option[Account] = None){}
object Todo extends SQLSyntaxSupport[Todo]{
val todo = syntax("todo")
override val tableName = "todo"
private val auto = AutoSession
def opt(m: ResultName[Todo])(rs: WrappedResultSet) = rs.longOpt(m.id).map(_ => Todo(m)(rs))
apply(todo.resultName)(rs)
def apply(a: ResultName[Todo])(rs: WrappedResultSet): Todo = new Todo(
id = rs.long(todo.id),
userId =rs.longOpt(todo.userId),
value = rs.string(todo.value)
)

def apply(m: ResultName[Todo], a: ResultName[Account])(rs: WrappedResultSet) = {
apply(m)(rs).copy(users = rs.longOpt(a.id).map(_ => Account(a)(rs)))
}


}

模型帐户是:

case class Account(id: Int, email: String, password: String, name: String, permission: Role,todos:Seq[Todo]=Nil)

object Account extends SQLSyntaxSupport[Account] {
...
val (a, t) = (Account.syntax, Todo.syntax)
val accounts: List[Account] = withSQL {
select.from(Account as a).leftJoin(Todo as t).on(a.id,t.userId)
}.one(Account(a))
.toMany(Todo.opt(t))
.map { (account, todos) => account.copy( todos = todos) }
.list.apply()
}
}

我得到的错误是:

[error] G:\testprojects\mifun\app\models\Todo.scala:23: overloaded method apply
needs result type
[error] apply(m)(rs).copy(users = rs.longOpt(a.id).map(_ => Account(a)(rs)))
[error] ^
[error] G:\testprojects\mifun\app\models\Account.scala:53: type mismatch;
[error] found : scalikejdbc.QuerySQLSyntaxProvider[scalikejdbc.SQLSyntaxSuppo
rt[models.Todo],models.Todo]
[error] required: scalikejdbc.ResultName[models.Todo]
[error] (which expands to) scalikejdbc.ResultNameSQLSyntaxProvider[scalikej
dbc.SQLSyntaxSupport[models.Todo],models.Todo]
[error] .toMany(Todo.opt(t))
[error] ^
[error] two errors found
[error] (compile:compile) Compilation failed

我有两个问题:

1、为什么我不能使用toMany?我想用ResultNameSQLSyntaxProvider,我写的opt函数怎么改?

2、Todo.scala:23 上的 rs 类型应该给什么?

最佳答案

1, why I can not use toMany? I want to use ResultNameSQLSyntaxProvider, how to change the opt function I wrote?

抱歉,这是由于文档中的错误。更新 opt 方法如下:

def opt(m: SyntaxProvider[Todo])(rs: WrappedResultSet) 
= rs.longOpt(m.resultName.id).map(_ => Todo(m.resultName)(rs))

或者像这样添加重载方法:

def opt(m: SyntaxProvider[Todo])(rs: WrappedResultSet): Option[Todo] = opt(m.resultName)(rs)

2, what rs type should give on Todo.scala:23?

正如错误消息所说,重载方法应该在 Scala 中定义为具有显式返回类型。

def apply(m: ResultName[Todo], a: ResultName[Account])(rs: WrappedResultSet) =  {
apply(m)(rs).copy(users = rs.longOpt(a.id).map(_ => Account(a)(rs)))
}

上面的方法应该是这样的:

def apply(m: ResultName[Todo], a: ResultName[Account])(rs: WrappedResultSet): Todo =  {
apply(m)(rs).copy(users = rs.longOpt(a.id).map(_ => Account(a)(rs)))
}

关于scala - 对于 scalikejdbc 如何编写带有外键约束的 QueryDSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24180458/

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