user3364192 user3364192 - 1 year ago 195
Scala Question

Slick - one to many table schema

suppose I have case classes as follows:

case class Warehouse(name:String,products:Seq[Product])
case class Product(name:String)

I would like to define a table which is binded to

class Warehouses(tag: Tag) extends Table[Warehouse](tag, "wareheosuses") {
def id = column[UUID]("id", O.PrimaryKey)
def name = column[String]("name")
def products = ???

I could define my table with tuples and reference
- that would be one to many relationship i need. However I need my Tables to reflect my domain classes so I can execute DBIO actions and get
not a tuple:

val warehouses = TableQuery[Warehouses] === 2).result.head) //this is of type Warehouse

How can I define schema from case class which contains collection?

Answer Source

Slick is not ORM

So, Slick does not support nested objects like hibernate. If you strongly want to have nested objects model Slick is not a suitable solution for your use case. This point is explicitly mentioned in the slick documentation as well.

Your use case in Slick is modelled like this

case class WarehouseModel(name:String, id: Long)
case class ProductModel(name:String, productId: Long)
case class WarehouseProductsModel(warehouseId: Long, productId: Long) 

WarehouseProductsModel captures the one-many relationship between Product and Warehouse

Note that above design looks like the typical database design one would choose to do in case of one-many relational. In Slick these models represents relational tables. So, for each table we need to have one model which represents that table in code. Slick helps in writing sql agnostic, composable, reusable, typesafe and scala collection like queries. So end of the day Slick code looks like Scala collections manipulation code.

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