I am practicing with Scala, Doobie and PostgreSQL. The database is within a Docker container. I am able to post and update jobs but unable to GET all posts. I keep getting the below error.
I have researched other similar questions but my differs as I am just trying to GET all from the database so I don't understand this column issue.
I am starting to think I need a circe encoder to read the Json out of the database??? The circe decoder below throws at error on leftMap and on show
implicit val get: Get[JobPostDetails] =
Get[Json].temap(_.as[JobPostDetails].leftMap(_.show))
case class JobPost(id: String, details: JobPostDetails)
case class JobPostDetails(title: String, description: String, salary: Double, employmentType: String, employer: String)
def createTable: doobie.Update0 = {
sql"""
|CREATE TABLE IF NOT EXISTS jobs (
| id TEXT PRIMARY KEY,
| details JSON NOT NULL
|)
""".stripMargin
.update
}
def getAll: doobie.Query0[JobPost] = {
sql"""
|SELECT id, details FROM jobs
""".stripMargin
.query[JobPost]
}
def getPosts: IO[List[JobPost]] = {
JobQueries.getAll.to[List].transact(xa)
}
09:01:50.452 [ioapp-compute-7] ERROR org.http4s.server.service-errors - Error servicing request: GET /api/v1/posts from 0:0:0:0:0:0:0:1
org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2.
at org.postgresql.jdbc.PgResultSet.checkColumnIndex(PgResultSet.java:2760) ~[postgresql-42.2.12.jar:42.2.12]
at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2780) ~[postgresql-42.2.12.jar:42.2.12]