user504909
user504909

Reputation: 9559

For scalikejdbc how to write QueryDSL with a foreign key constraint

I use scalikejdbc 2.0.1 and playframework 2.3. I followed the instruction of One-to-many http://scalikejdbc.org/documentation/one-to-x.html, but there is still an error:

My data model is, one Account with many Todo's:

Todo model:

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)))
    }


}

The model Account is:

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()
    }
}

I get the error is:

[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

I have two questions:

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

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

Upvotes: 3

Views: 1129

Answers (1)

Kazuhiro Sera
Kazuhiro Sera

Reputation: 1877

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

Sorry, this is due to a mistake in documentation. Update the opt method as follows:

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

Or add overloaded method like this:

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

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

As the error message says, overloaded method should be defined with explicit return type in Scala.

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

The above method should be like this:

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

Upvotes: 2

Related Questions