/ Scala

What you need to know about Slick


Slick is not an ORM. Its a FRM(Functional-Relational Mapping).




You can describe schema's in 2 ways, by using code generator or by manually coding them in tables.scala file. If you already have an existing database or taking a lazy route of using something like mysql workbench to create the databases then use the method 1 code generator. If its a greenfield project go with coding them manually

Method 2: Coding manually

class Categories(tag: Tag) extends Table[(Int, String, String)](tag, "Categories") {
  def category_id:Rep[Int] = column[Int]("item_id", O.PrimaryKey)
  def category:Rep[String] = column[String]("category")
  def description = column[String]("description")

  def * = (Int, String, String)
val categories = TableQuery[Categories]

class Items(tag: Tag) extends Table[(Int, String)](tag, "Items") {
  def id:Rep[Int] = column[Int]("item_id", O.PrimaryKey)
  def item:Rep[String] = column[String]("item")
  def category_id = column[Int]("category_id")

  def * = (Int, String)
  def category = foreignKey("category_fk", category_id, categories)(_.category_id)
val items = TableQuery[Items]



create the tables from schema first

val createTables = DBIO.seq(
  (categories.schema ++ items.schema).create

val seedDatabase = DBIO.seq(
  categories += (1, "Daily TODO", "List of items on daily todo"),
  items += (1, "7 min workout", 1)


val query = items.map(_.item)
val action = query.result

### Lifted Embedding
In `direct embedding` the database columns types are matched with a scala's datatypes like `double <-> double`.
In `lifted embedding` the database columns are matched with types `lifted` into a `Rep` constructor. 
eg: `double <-> Rep[Double]`