question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PostgreSql json error

See original GitHub issue

In Ktorm 3.3,

I have a postgresql database with a table that has a column named ‘template’ that has a datatype of json.

import org.ktorm.jackson.json
import org.ktorm.schema.Table

object TestTable: Table<TestTable>("test_table") {
    val id = varchar("id").primaryKey().bindTo { it.id }
    val template = json<Template>("template").bindTo { it.template }
}

interface TestTable: Entity<TestTable> {
    companion object: Entity.Factory<TestTable>()
    var id: String
    var template: Template?
}

I am able to successfully pull the model including the json field using the below query

fun findById(id: String): TestTable? { return database.sequenceOf(TestTable) .find { it.id eq id } }

However when I try to patch the table using the below query I get the following error

fun insertOrUpdateById(id: String, testTable: TestTable): Int {
        return database.insertOrUpdate(TestTable) {
            set(it.template, testTable.template)
            onConflict {
                set(it.template, testTable.template)
            }
        }
    }
Ktorm; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: column \"template\" is of type json but expression is of type character varying\n  Hint: You will need to rewrite or cast the expression.\n  Position: 59

Do I need to do something specific in order to update the json value?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Tmillycommented, Apr 7, 2021

Hey Vincent, thanks for the quick responses. I had to stub a lot of the data in my example due to it being corporate code so if it’s not entirely syntactically correct, thats why. Also I very much appreciate you telling me what the culprit could be otherwise I wouldn’t have been able to come up with the following workaround.

I created a custom SqlType for postgres’s Json type, the only difference I made was to the doSetParameter function, the rest of the class is the same implementation as the default JsonSqlType.

import org.postgresql.util.PGobject

override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: T) {
    val jsonObject = PGobject()
    jsonObject.apply {
        type = "json"
        value = objectMapper.writeValueAsString(parameter)
    }
    ps.setObject(index, jsonObject)
}

This did the trick!

0reactions
vincentlauvlwjcommented, May 10, 2021

@hurui200320 Fixed in 3.4.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

syntax error when trying to query json documents in postgresql
But when I tried to select, got syntax error: template1=# select * from jsonTest where data->>city ...
Read more >
12: 9.15. JSON Functions and Operators - PostgreSQL
When you query JSON data, the path expression may not match the actual JSON data structure. An attempt to access a non-existent member...
Read more >
PostgreSQL: Insert into JSONB key failing with syntax error at ...
The reason is that the attributes of a JSON column are not database columns, so you cannot use them in the SET clause...
Read more >
How to use the JSON Datatype in PostgreSQL - Data Virtuality
Learn to use the JSON datatype in PostgreSQL, using its great functionality to work with JSON ... An error is thrown by PostgreSQL...
Read more >
Error Source — PostgREST 9.0.0 documentation
PostgREST will convert the MESSAGE , DETAIL , HINT and ERRCODE from the PostgreSQL error to JSON format and add an HTTP status...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found