Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quill ignores JsonValue type on insert when it is nested in Option and None is being inserted. #283

Open
heaven-born opened this issue Jun 13, 2023 · 1 comment

Comments

@heaven-born
Copy link

heaven-born commented Jun 13, 2023

Version: 4.6.0.1
Module: quill-jdbc-zio
Database: postgress

This is an example of the code with the issue:

 case class Person(id: Int, age: Int, data: Option[JsonValue[String]])
 val person = Person(1,20, None)

 query[Person].insertValue(lift(person)).returningGenerated(_.id)

Table:

CREATE TABLE person (
    id BIGSERIAL PRIMARY KEY,
    age INTEGER NOT NULL,
    data JSON
)

Expected behavior

I expect this record to be inserted into postgress DB.

Actual behavior

The request is throwing error: ERROR: column "data" is of type json but expression is of type character varying

Workaround (example for jsonb)

  given [A]: Encoder[Option[JsonbValue[A]]] = JdbcEncoder[Option[JsonbValue[A]]](
    java.sql.Types.OTHER,
    (index: Index, maybeValue: Option[JsonbValue[A]], row: PrepareRow, Session) => {
      if(maybeValue.isEmpty)
        row.setObject(index, null, java.sql.Types.OTHER)
      else
        row.setObject(index, maybeValue.get.value, java.sql.Types.OTHER)
      row
    }
  )
@alexander-klimov
Copy link

alexander-klimov commented Jul 9, 2023

I've added this encoder to make it work (Scala 3):

given[T](using jsonEncoder: JsonEncoder[T]): Encoder[JsonbValue[T]] =
  encoder(Types.OTHER, (index, jsonValue, row) => {
    val obj = new org.postgresql.util.PGobject()
    obj.setType("jsonb")
    val jsonString = jsonEncoder.encodeJson(jsonValue.value, None).toString
    obj.setValue(jsonString)
    row.setObject(index, obj)
  })

And I think it should be like this out of the box.
JSON/JSONB should be the default. If someone wants to use VARCHAR, they can override the default encoder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants