我目前收到此错误,我无法弄清楚原因:
exception during macro expansion: java.lang.IllegalStateException: Can't tokenize a non-scalar lifting. AgentService.this.agentsByOrganization(id).id
在进行查询之前,我是否需要将ID转换为Long
?我希望能够使用特定的Id类,但我是Scala的新手,不确定是否可行。此外,并非所有查询都失败。例如,delete
可以正常工作,即使它也传递了AgentId
。而findByOrganization
方法不起作用。传递AgentId
的其他方法也显示与findByOrganization
相同的错误。
模型:
case class AgentId(value: Long) extends AnyVal
case class OrganizationId(value: Long) extends AnyVal
case class Agent(
id: AgentId
, identifier: String
, organizationId: OrganizationId
, createdAt: LocalDateTime
, updatedAt: LocalDateTime
)
服务:
class AgentService(implicit val ex: ExecutionContext, val ctx: DBContext)
extends AgentsRepository {
import ctx._
def listByOrganization(id: OrganizationId): List[Agent] =
ctx.run(agentsByOrganization(id)) // this returns the error
def delete(agent: RichAgent): Unit = {
ctx.run(deleteAgent(agent)) // this doesn't
}
}
存储库:
trait AgentsRepository extends Repository {
import ctx._
def agentsByOrganization(id: OrganizationId) = quote { // error
query[Agent].filter(_.organizationId == lift(id))
}
def agentById(id: AgentId) = quote {
query[Agent].filter(_.id == lift(id))
}
def deleteAgent(agent: Agent) = quote { agentById(agent.id).delete }
}
分贝
import io.getquill.{PostgresJdbcContext, SnakeCase}
package object db {
class DBContext(config: String) extends PostgresJdbcContext(SnakeCase, config)
trait Repository {
val ctx: DBContext
}
}
我见过这个similar issue,但它似乎特定于Option
。这是一个scastie snippet。
答案 0 :(得分:1)
不太确定发生了什么,但现在它正在发挥作用:
case class AgentId(value: Long) extends AnyVal
case class Agent(
id: AgentId
, identifier: String
, organizationId: OrganizationId
, createdAt: LocalDateTime
, updatedAt: LocalDateTime
)
case class RichAgent(
id: AgentId
, identifier: String
, organization: Organization
)
服务
class AgentService(implicit val ex: ExecutionContext, val ctx: DBContext)
extends AgentsRepository {
import ctx._
def listByOrganization(id: OrganizationId): List[Agent] =
ctx.run(agentsByOrganization(id))
def delete(agent: Agent): AgentId = {
AgentId(ctx.run(deleteAgent(agent.id)))
}
}
存储库
trait AgentsRepository extends Repository {
import ctx._
val agents = quote {
query[Agent]
}
def agentsByOrganization(id: OrganizationId) = quote {
agents.filter(_.organizationId == lift(id))
}
def agentById(id: AgentId) = quote {
agents.filter(_.id == lift(id))
}
def deleteAgent(agentId: AgentId) = quote { agentById(agentId).delete }
}
分贝
object db {
class DBContext(config: String) extends PostgresJdbcContext(SnakeCase, config)
trait Repository {
val ctx: DBContext
}
}