Skip to content

Instantly share code, notes, and snippets.

@timcharper
Created February 6, 2014 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timcharper/8836079 to your computer and use it in GitHub Desktop.
Save timcharper/8836079 to your computer and use it in GitHub Desktop.
import scala.slick.driver.H2Driver.simple._
case class Person(id: Int, name: String)
class PersonTable(tag: Tag) extends Table[Person](tag, "Person") {
def id: Column[Int] = column[Int]("id", O.PrimaryKey, O.AutoInc) // This is the primary key column
def name: Column[String] = column[String]("name")
// Every table needs a * projection with the same type as the table's type parameter
def * = (id, name) <> (Person.tupled, Person.unapply)
}
// A Suppliers table with 6 columns: id, name, street, city, state, zip
case class Pet(id: Int, personId: Int, kind: String)
class PetTable(tag: Tag) extends Table[Pet](tag, "Pet") {
def id: Column[Int] = column[Int]("id", O.PrimaryKey, O.AutoInc) // This is the primary key column
def personId: Column[Int] = column[Int]("personId")
def kind: Column[String] = column[String]("kind")
// Every table needs a * projection with the same type as the table's type parameter
def * = (id, personId, kind) <> (Pet.tupled, Pet.unapply)
}
// The main application
object MySlick extends App {
// The query interface for the Suppliers table
val people = TableQuery[PersonTable]
val pets = TableQuery[PetTable]
// Create a connection (called a "session") to an in-memory H2 database
Database.forURL("jdbc:h2:mem:hello", driver = "org.h2.Driver").withSession { implicit session =>
// Create the schema by combining the DDLs for the Suppliers and Coffees tables using the query interfaces
(people.ddl ++ pets.ddl).create
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment