Vakindu
Vakindu

Reputation: 815

Scala Akka Postgresql doobie Error: Type Mismatch Found Seq, Required String

I'm building an Akka scala application powered by Postgresql and doobie.

I'm trying to fetch all the rows from a postgresql table named Terrain_registry where the value of the parent column matches the value of id passed through the dbGetTerrain(xa: Transactor, id: String) function.

However the current code keeps giving me the error below.


Type Mismatch Error: D:\Studio\WORK\akka\AKKA-HTTP\scala-akka-http-terrain-IoT\src\main\scala\com\example\TerrainRegistry.scala
sql"SELECT id, parent, waterPercentage, landPercentage FROM Terrain_registry WHERE parent = ${id}".
Found:    Seq[com.example.Terrain]
Required: String
query[Terrain].
to[Seq].
transact(xa).
unsafeRunSync()

My current code looks like below:

Terrain_registry Postgresql table:


CREATE TABLE IF NOT EXISTS Terrain_registry (
  id INT PRIMARY KEY,
  parent VARCHAR(50) NOT NULL,
  waterPercentage DECIMAL(10, 2) NOT NULL,
  landPercentage DECIMAL(10, 2) NOT NULL
);

Scala code:


final case class Terrain(id: String, parent: String, waterPercentage: Double, landPercentage: Double)

type Transactor = doobie.Transactor.Aux[IO, Unit]
  def transactor(pgConfig: PostgresConfig): Transactor = {
    Transactor.fromDriverManager[IO](
      "org.postgresql.Driver", pgConfig.url, pgConfig.user, pgConfig.pass
    )
}

def dbGetTerrain(xa: Transactor, id: String): Terrain = {
    Terrain(
      sql"SELECT id, parent, waterPercentage, landPercentage FROM Terrain_registry WHERE parent = ${id}".
      query[Terrain].
      to[Seq].
      transact(xa).
      unsafeRunSync()
    )
}

Json request that passes id to dbGetTerrain function:


{
    "id":"1",
    "parent": "P1",
    "waterPercentage": 70.0, 
    "landPercentage": 30.0
}

Upvotes: 0

Views: 31

Answers (0)

Related Questions