1

多数の小さな列と大きな (たとえば BLOB) 列を含むテーブルがあるとします。

  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)

  }

状況によっては、列を除くすべての列についてテーブルをクエリしたいと思いlargeます。他にも、入れたいと思います。タプルよりもケースクラスを使用することを好みます。

これを行うためのSlickに良いパターンはありますか?

私が検討したオプション:

  1. 「スキニー」マッピングと「ファット」マッピングの 2 つのマッピングがあります。
  2. 大きな列を別のテーブルに分割し、必要に応じて結合します。
4

1 に答える 1

-1

ここで必要なのは、フィールドのサブセットのみを選択できるようにするmap機能だと思います。TableQueryだから、このようなもの:

case class Thing(id: Int, small1: String, small2: String, small3: String, large: String)
case class LiteThing(id: Int, small1: String, small2: String, small3: 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)
}
val things = TableQuery[ThingMapping]

val liteThingQuery = things.map(t => LiteThing(t.id, t.small1, t.small2, t.small3))

そこで、列LiteThingを除くフィールドのサブセットを表す、という名前の別のケース クラスを追加しました。large次に、そのフィールドをmap選択しない新しいクエリを作成するために使用し、 . 私はこれをコンパイルしていませんが、これがあなたが進みたい方向であると確信しています.Hello Slick Activator Templateの「特定の列の選択」セクションからこれを取得しました(チュートリアル情報を完全に展開した後)。largeLiteThing

次のような代替手段で遊ぶことができます

def small = (id, small1, small2, small3)
def * = (small, large)

また

def small = (id, small1, small2, small3)
def * = small ~ large <> (Thing.tupled, Thing.unapply)

そして使う

things.map(_.small)
于 2014-09-01T12:43:02.190 に答える