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 => === toEventLink.toId)
} yield event

In: === toEventLink.toId
is an ID (extends MappedTo[Long]) and
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 =>[Long] === toEventLink.toId

There is a an issue open to make a more general solution for this:

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