Transient values in case classes

240 views
Skip to first unread message

Serkan Camurcuoglu

unread,
Oct 29, 2014, 10:25:56 AM10/29/14
to scala...@googlegroups.com
Hi,
I'm a Scala/Slick newbie, and I have a schema question. I'm mapping my tables to case classes. However, I'm using the same classes for JSON mapping, and I want to have some optional values (which do not exist as db columns) on these classes. (What I actually want is the feature called @Transient fields in Hibernate/JPA terminology). However, I cannot get my code to compile when I simply want to use Option[] fields, I get the following error (which is quite obvious):

[error] Slick does not know how to map the given types.
[error] Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List).

Is there any workaround to this? I can understand that I can write custom versions of tupled and unapply to workaround this problem, however I don't know how to implement these methods either.

Any help would be greatly appreciated,

virtualeyes

unread,
Oct 29, 2014, 1:51:37 PM10/29/14
to scala...@googlegroups.com
Define the optional fields in your table classes but don't include them in your projection(s).

class FooTable[...]{
 
def id: Column[Int]
 
...
 
def notdbField: Option[String]

 
def * = (id,...) // don't specify notdbField in the projection
}

Serkan Camurcuoglu

unread,
Oct 29, 2014, 5:16:33 PM10/29/14
to scala...@googlegroups.com
Hi,
I tried that but I can't do this, because the error I get is that the tuple does not match the case class companion object's apply method.

Anyway I ended up defining a custom converter object with methods tupleToObject and objectToTuple, and they seem to work fine for now. Thanks for the help.
Reply all
Reply to author
Forward
0 new messages