Description
I am experiencing a few bugs related to the format of a value inserted into a JSONB column varying. The results are non-deterministic—I can run the same query with the same values back to back, and it only occasionally winds up formatted differently.
In one bug, the value (a nested JS object) is stringified before insertion into the column as an escaped string (e.g., "{\"foo\": \"bar\"}"
). The query for this is pretty simple. INSERT INTO my_table ${sql(data)} RETURNING id
. This is happening about 3% of the time.
In another I'm doing a merge with an existing object value and it's winding up an array somehow. This is happening less often—about 0.1% of the time. The SQL query for this is again relatively simple, leaning on sql
to do the heavy lifting:
UPDATE users
SET metadata = metadata || ${sql.json(data)}
WHERE id = ${id}
Since I don't know where this is coming from and it's so infrequent it is hard to put together a reproduction, but I am using Zod to parse and validate all inputs before they're passed to sql
, so I am reasonably sure that it is not merely the input being in the wrong format (e.g., no typeof value === "object"
letting arrays slip through).
If there are any things I can do to capture additional information or avoid this even without fully knowing what is happening I am all ears. 🤞 🙏