Search code examples
postgresqlinsert-update

Getting error "could not identify an equality operator for type json" with insert on conflict


When doing a bulk upload into a table that has a json column, I am getting the error "could not identify an equality operator for type json". The json column is not part of any comparison (as far as I can tell), so I am mystified as to why I am getting the error.

Looking at the data being inserted, this everything appears correct.

The table is:

create table foo (
    c0  serial not null,
    c1  int4 not null,
    c2  timestamp not null,
    c3  timestamp not null,
    c4  bool not null
    c5  char(1) not null default 'I',
    c6  json not null default '[]'::json,

    constraint foo_pkey primary key (c0)
);

create unique index foo_idx on foo using btree (c1, c2);

And the python code using psycopg2 is:

def upsert_foo(cursor, data):
    sql = """
        INSERT INTO foo
            (c1, c2, c3, c4, c5, c6)
        VALUES
            (%s,%s,%s,%s,%s,%s)
        ON CONFLICT (c1, c2)
        DO UPDATE SET
            c3 = %s,
            c4 = %s,
            c5 = %s,
            c6 = %s;
        """

    execute_batch(cursor, sql, data)

The full error is:

psycopg2.errors.UndefinedFunction: could not identify an equality operator for type json

Solution

  • The problem was the trigger on the table that had a when clause. This failed because the table has a json column, which cannot be compared. By removing the when clause from the trigger, the problem went away.