Skip to content

Instantly share code, notes, and snippets.

@ayushmishra2005
Last active September 9, 2020 14:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ayushmishra2005/d382801e0cb2d0df9e7c to your computer and use it in GitHub Desktop.
Save ayushmishra2005/d382801e0cb2d0df9e7c to your computer and use it in GitHub Desktop.
Store Scala Collection Into PostgreSQL using Slick

Open PostgreSQL and Create a table student in database student.

CREATE EXTENSION hstore;

CREATE TABLE student (
    id     int,
    name   varchar(254) NOT NULL,
    hobbies  text[],
    marks hstore
);

To use slick-pg in sbt project, add the following to your project file:

libraryDependencies += "com.github.tminglei" %% "slick-pg" % "0.8.1"

To run

sbt run

In this example, we have inserted only Scala List and Map in PostgreSQL table. To insert other types, please go here https://github.com/tminglei/slick-pg.

import slick.driver.PostgresDriver
import com.github.tminglei.slickpg._
/**
* This class has the functionality to store
* scala collection into postgreSQL
*
* For Example:- Suppose you have a table with columns having data types such as hstore(sets of key/value pairs) and Arrays
*
* CREATE EXTENSION hstore;
*
* CREATE TABLE student (
* id int,
* name varchar(254) NOT NULL,
* hobbies text[],
* marks hstore
* );
*
* Then you can insert scala list in postgres Array and scala map in postgres hstore using this example.
*
* Here we have used Slick-pg [https://github.com/tminglei/slick-pg] (Slick extensions for PostgreSQL),
* which also supports other pg types.
*
*/
trait MyPostgresDriver extends PostgresDriver
with PgArraySupport
with PgHStoreSupport {
override lazy val Implicit = new ImplicitsPlus {}
override val simple = new SimpleQLPlus {}
trait ImplicitsPlus extends Implicits
with ArrayImplicits
with HStoreImplicits
trait SimpleQLPlus extends SimpleQL
with ImplicitsPlus
}
object MyPostgresDriver extends MyPostgresDriver
import MyPostgresDriver.simple._
/**
* Student Details
*/
case class Student(
id: Int,
name: String,
hobbies: List[String],
marks: Map[String, String])
/**
* Slick mapping of Student
*/
class StudentTable(tag: Tag) extends Table[Student](tag, "student") {
def id = column[Int]("id", O.PrimaryKey)
def name = column[String]("name")
def hobbies = column[List[String]]("hobbies", O.Default(Nil))
def marks = column[Map[String, String]]("marks")
def * = (id, name, hobbies, marks) <> (Student.tupled, Student.unapply)
}
/**
* Trait for RDS database connection details
*/
trait RdsDbDetailsStudent {
lazy val studentQuery: TableQuery[StudentTable] = TableQuery[StudentTable]
val db = Database.forURL("jdbc:postgresql://localhost:5432/student", "root", "root",
null, "org.postgresql.Driver")
}
object StudentData extends Application with RdsDbDetailsStudent {
val student = Student(1, "Ayush", List("Study", "Coding"), Map("Scala" -> "90", "Java" -> "80", "PHP" -> "0"))
db.withSession { implicit session =>
studentQuery += student
}
println("storing-----------------")
}
@frecano
Copy link

frecano commented Jun 28, 2016

What happens if hobbies is Null, even if I used O.Default(Nil) it throws [SlickException: Read NULL value (null) for ResultSet column ]

@Dhirendra12
Copy link

getting error
Error: Could not find or load main class

@Shrutihegde13
Copy link

I am facing the same problem for h2 database. Do you have any idea?

@bettdouglas
Copy link

I am facing the same problem for h2 database. Do you have any idea?

This slick extension is for postgres. Try running it with a Postgres database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment