Created
February 6, 2014 00:16
-
-
Save timcharper/8836079 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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