tminglei / slick-pg

Slick extensions for PostgreSQL
BSD 2-Clause "Simplified" License
838 stars 180 forks source link

cannot be cast to slick.ast.OptionType #274

Open maurerbot opened 8 years ago

maurerbot commented 8 years ago

I'm trying to build a query that returns a few selected columns using slick-pg for as my slick driver but I'm having issues trying to map on the json column MyValueObject. The error I am getting at runtime is...

A client error occurred: slick.driver.JdbcTypesComponent$MappedJdbcType$$anon$1 cannot be cast to slick.ast.OptionType"

My case case clase, table mapper, and query looks similar to this...

case class MyAggregateObject(foo: String, valueObj: Option[MyJsonObject)
case class MyJsonObject(bar: Int, baz: Option[String] = None)
implicit val jsonObjectMapper = MappedColumnType.base[Option[MyJsonObject], JsValue](s => Json.toJson(s),str => Json.fromJson[MyJsonObject](str).asOpt)
val q = for {
  list <- myAggregateObjects
} yield (
  prs.foo,
  prs.MyJsonObject.flatMap(_.baz.bind)) // throws the error!

The only solution I can come up with right now is to map on the result but the returning rows can be very large and I'd like to avoid loading a sequence of large objects into memory.

Is there a way to fix this query?

stackoverflow: http://stackoverflow.com/questions/36703548/slick-query-jsonb-cannot-be-cast-to-slick-ast-optiontype

tminglei commented 8 years ago

I don't know more details about your case, so I can't reproduce it on my local.

But, why you don't declare the jsonObjectMapper as:

implicit val jsonObjectMapper = MappedColumnType.base[MyJsonObject, JsValue](s => Json.toJson(s),str => Json.fromJson[MyJsonObject](str))

As you know, slick will generate the option version of the mapper on demand for us.

maurerbot commented 8 years ago

just drop the optional? I have it optional atm because the aggregate is created first without any data. I think I can work around that and try.

tminglei commented 8 years ago

I don't know what exactly happened, so I just pointed out an obviously problem. You can try it first.

If the problem still exists, pls provide more full details, to let me reproduce and deep it.

djx314 commented 8 years ago

@adrianmaurer All of the BaseTypedType can not to be Option[_], it's hard code in slick. Because can't read null data in all BaseTypedType. If you defined a Option BaseTypedType slick will always throws a error. Just do as what @tminglei said and slick will auto import a Shape[Rep[Option[MyJsonObject], Option[MyJsonObject], Rep[Option[MyJsonObject]]]] to you.