Etam Etam - 4 years ago 109
Scala Question

Compare MappedTo with raw type in Slick Query

How do you compare MappedTo[T] with raw T columns?

I have a problem (Cannot perform option-mapped operation) with this code:

for {
toEventLink <- Link.linksFromQuery(fromEntity).filter(_.toTable === Event.tableName)
event <- Event.table.filter(e => e.id === toEventLink.toId)
} yield event


In:
e.id === toEventLink.toId
where
e.id
is an ID (extends MappedTo[Long]) and
toEventLink.toId
is a raw Long.

Answer Source

This compiler check is doing what it’s supposed to do (e.g., not let you accidently compare an ID against something that isn’t an ID). But I can totally see why this would be useful (e.g., when migrating a schema to start to used typed keys).

You can use asColumnOf to cast a column to the type you want. For example:

e => e.id.asColumnOf[Long] === toEventLink.toId

There is a an issue open to make a more general solution for this: https://github.com/slick/slick/issues/1664

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download