Suppose I have a table with a number of small columns, and a large (say BLOB) column:
case class Thing(id: Int, small1: String, small2: String, small3: String, large: String)
class ThingMapping(tag: Tag) extends Table[Thing](tag, "things") {
def id = column[Int]("id", O.PrimaryKey, O.NotNull, O.AutoInc)
def small1 = column[String]("small1")
def small2 = column[String]("small2")
def small3 = column[String]("small3")
def large = column[String]("large")
def * = (id, small1, small2, small3, large) <> (Thing.tupled, Thing.unapply)
}
Under some circumstances, I'd like to query the table for all the columns except the large
column. In others, I'd like to include it. I prefer to use case classes rather than tuples.
Is there good pattern in Slick for doing this?
Options I've considered:
- Having two mappings -- a "skinny" and "fat" mapping.
- Splitting out the large column into a separate table, then joining it in if required.
I think what you need here is the
map
function on yourTableQuery
to allow you to select only a subset of fields. So something like this:So I added another case class called
LiteThing
that represents the subset of fields, excluding thelarge
column. I then usemap
to create a new query that will not select thatlarge
field and it maps to aLiteThing
. I have no compiled this, but I'm pretty sure this is the direction you want to go in. I got this from the Hello Slick Activator Template, in the section "Selecting Specific Columns" (after fully expanding the tutorial info).You can play around with alternatives like
or
And use