I'm currently getting this error and I can't quite figure out why:
exception during macro expansion: java.lang.IllegalStateException: Can't tokenize a non-scalar lifting. AgentService.this.agentsByOrganization(id).id
Do I need to convert the Ids to Long
prior to making the query? I'd like to be able to use the specific Id class, but I'm new to Scala and not sure if that is possible. In addition, not all of the queries fail. For example, delete
works, even though it is also being passed an AgentId
. Whereas the findByOrganization
method does not work. Other methods that are passed an AgentId
are also showing the same errors as findByOrganization
.
model:
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
)
service:
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
}
}
repository:
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 }
}
db
import io.getquill.{PostgresJdbcContext, SnakeCase}
package object db {
class DBContext(config: String) extends PostgresJdbcContext(SnakeCase, config)
trait Repository {
val ctx: DBContext
}
}
I've seen this similar issue , but it seems specific to Option
. Here's a scastie snippet .
Not quite sure what happened, but now it is working:
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
)
service
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)))
}
}
repository
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 }
}
db
object db {
class DBContext(config: String) extends PostgresJdbcContext(SnakeCase, config)
trait Repository {
val ctx: DBContext
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.