Skip to content

Distinguish jsonb[] array columns from scalar jsonb in ComplexHelper#13

Merged
aersam merged 2 commits into
mainfrom
fix-jsonb-array-upsert
Jul 17, 2026
Merged

Distinguish jsonb[] array columns from scalar jsonb in ComplexHelper#13
aersam merged 2 commits into
mainfrom
fix-jsonb-array-upsert

Conversation

@aersam

@aersam aersam commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

information_schema.columns surfaces both a plain jsonb column holding a JSON-array value and a jsonb[] (Postgres ARRAY of jsonb) column identically from the Python side: both hand recursive_convert a bare Python list. _load_complex_type_from_colinfos previously collapsed both cases to the same Jsonb sentinel, so recursive_convert always wrapped the whole incoming list in one Jsonb(...) -- correct for the scalar-column case, but wrong for jsonb[]: psycopg then tries to bind a single jsonb value to an array column, and Postgres raises DatatypeMismatch ("column ... is of type jsonb[] but expression is of type jsonb").

Adds a distinct JsonbArray sentinel for the array-of-jsonb case, so recursive_convert can tell "this list is the array" (wrap each element in its own Jsonb(...)) apart from "this list is JSON content for one scalar jsonb value" (wrap the whole thing once, unchanged from before).

Found via ccmt2's pgdevkit migration: printing.printtemplates.articles_to_print is a jsonb[] column, and pg_upsert_dict's INSERT started failing with this exact DatatypeMismatch once ccmt2's postgres.py moved onto pgdevkit.db.crud/ComplexHelper.

aersam added 2 commits July 17, 2026 15:20
information_schema.columns surfaces both a plain `jsonb` column holding a
JSON-array *value* and a `jsonb[]` (Postgres ARRAY of jsonb) column
identically from the Python side: both hand recursive_convert a bare
Python list. _load_complex_type_from_colinfos previously collapsed both
cases to the same `Jsonb` sentinel, so recursive_convert always wrapped the
whole incoming list in one `Jsonb(...)` -- correct for the scalar-column
case, but wrong for jsonb[]: psycopg then tries to bind a single jsonb
value to an array column, and Postgres raises DatatypeMismatch ("column
... is of type jsonb[] but expression is of type jsonb").

Adds a distinct JsonbArray sentinel for the array-of-jsonb case, so
recursive_convert can tell "this list *is* the array" (wrap each element
in its own Jsonb(...)) apart from "this list is JSON content for one
scalar jsonb value" (wrap the whole thing once, unchanged from before).

Found via ccmt2's pgdevkit migration: printing.printtemplates.articles_to_print
is a jsonb[] column, and pg_upsert_dict's INSERT started failing with this
exact DatatypeMismatch once ccmt2's postgres.py moved onto
pgdevkit.db.crud/ComplexHelper.
@aersam
aersam merged commit a353f70 into main Jul 17, 2026
1 check passed
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

Successfully merging this pull request may close these issues.

1 participant