Skip to content

Inserting a json.RawMessage no longer works with v1.11.0 #1254

@zeisss

Description

@zeisss

We are using SQLC to insert some JSON into a column of type JSON with Postgres 16. SQLC generates insert statements using json.RawMessage. This worked with lib/pq v1.10.9 and earlier but fails now with v1.11.0. I am not seeing anything in the release notes that explains this, so I am raising an issue in case this is unintended.

The following test works with v1.10.9 checked out but fails when upgrading to v1.11.0 (and recreating the postgres db from docker-compose):

package pq

import (
	"bytes"
	"encoding/json"
	"testing"
)

func TestInsertJSONRawMessage(t *testing.T) {
	// db := pqtest.MustDB(t)
	db := openTestConn(t)
	defer db.Close()

	_, err := db.Exec("CREATE TABLE IF NOT EXISTS test_json (id SERIAL PRIMARY KEY, data JSON)")
	if err != nil {
		t.Fatalf("failed to create table: %v", err)
	}

	_, err = db.Exec("INSERT INTO test_json (data) VALUES ($1)", json.RawMessage(`{"name": "John", "age": 30}`))
	if err != nil {
		t.Fatalf("failed to insert data: %v", err)
	}

	var data json.RawMessage
	err = db.QueryRow("SELECT data FROM test_json WHERE id = 1").Scan(&data)
	if err != nil {
		t.Fatalf("failed to select data: %v", err)
	}

	if !bytes.Equal(data, json.RawMessage(`{"name": "John", "age": 30}`)) {
		t.Fatalf("expected %s, got %s", json.RawMessage(`{"name": "John", "age": 30}`), data)
	}
}

v1.10.9

% PGHOST=localhost PGPORT=5432 PGUSER=postgres PGSSLMODE=disable PGDATABASE=postgres go test -run TestInsertJSONRawMessage
PASS
ok  	github.com/lib/pq	0.422s

v1.11.0

After switching the tag, swapping in pqtest.MustDB() in the test and deleting + recreating the docker-compose stack:

% PQTEST_BINARY_PARAMETERS=1 go test -run TestInsertJSONRawMessage
--- FAIL: TestInsertJSONRawMessage (0.04s)
    bug_test.go:23: failed to insert data: pq: invalid input syntax for type json (22P02)
FAIL
exit status 1
FAIL	github.com/lib/pq	0.478s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions