Postgres INSERT ON CONFLICT with JSONB

Randy Layman

I'm trying to use Postgres as a document store and am running into a problem when I'm trying to effectively upsert a document where the Postgres parser doesn't seem to like the JSONB operator.

I have a table:

CREATE TABLE tbl (data jsonb NOT NULL);
CREATE UNIQUE INDEX ON tbl ((data->>'a'));

and I try to insert data with:

INSERT INTO tbl (data) VALUES ('{ "a": "b" }'::jsonb) 
  ON CONFLICT (data->>a) 
  DO UPDATE SET data = data || '{ "a": "b" }'::jsonb

I get this error message:

ERROR:  syntax error at or near "->>"

I've tried data->>a, data->>'a', data->a, and maybe data->'a'. All of those are

I'd like to leave the identifier column (a in the example) within the JSON and not make it a column on the table.

Is what I'm trying to do currently supported?

Nick

There are two issues you have:

1) You need to add additional parenthesis, like so:

ON CONFLICT ((data->>'a'))

2) You need to preface the last data reference with your table alias, like so:

DO UPDATE SET data = tbl.data || '{ "a": "b" }'::jsonb

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Postgres INSERT ON CONFLICT DO NOTHING vs SELECT + INSERT query

分類Dev

Postgres INSERT ON CONFLICT DO UPDATE vsINSERTまたはUPDATE

分類Dev

Postgres INSERT ON CONFLICT DO NOTHING vs SELECT + INSERTクエリ

分類Dev

Ordering Postgres jsonb in Rails

分類Dev

Querying a jsonb array in postgres

分類Dev

JSONBを使用したPostgresINSERT ON CONFLICT

分類Dev

これは、PostgresでINSERT ON CONFLICTを一括する正しい方法ですか?

分類Dev

Postgres sum of array stored in jsonb

分類Dev

Postgres find in jsonb nested array

分類Dev

Postgres 9.5 ON CONFLICT DO SELECT

分類Dev

postgres jsonb_set multiple keys update

分類Dev

postgres jsonb_set multiple keys update

分類Dev

postgres jsonb_set multiple keys update

分類Dev

Postgres array_to_string() with array in JSONB field

分類Dev

Postgres配列列とJSONB列

分類Dev

Check if Postgres jsonb contains one of the values

分類Dev

jsonbのPostgres集計関数

分類Dev

Postgres join tables array JSONB column

分類Dev

Query for multiple array elements in Postgres JSONB column

分類Dev

Insert POINT into postgres database

分類Dev

Insert JSON into Postgres 9.5

分類Dev

Get rid of all empty strings values in jsonb | Postgres

分類Dev

JSONB []列postgres内の値を返す

分類Dev

PostgresのJSONとJSONBの違い

分類Dev

jsonb postgres配列から要素を選択

分類Dev

Query Postgres JSONB where key doesn't exist

分類Dev

Access (and count) just object values from Postgres JSONB array of objects

分類Dev

postgresで行をjsonbとして集約

分類Dev

Storing Postgres Array of Jsonb in Rails 5 Escapes Strings Unexpectedly

Related 関連記事

ホットタグ

アーカイブ