Open ddembo opened 1 month ago
For additional context, see issue #889; we attempted to use a transformation on undefined values to work around the problems encountered when inserting records where some records have non-default values for properties supplied, but other records have the same property undefined & fall back to the default db value is desired.
I'm not sure if this is a bug, or incorrect implementation, but i've done my best to follow the docs closely, and am not able to achieve the expected/desired behaviour.
In our application, when using postgres.js to insert records that can contain undefined values, we want the default behaviour to be that those fields use the column's DEFAULT value when being inserted.
When inserting values directly (e.g. in Postico), it is acceptable to provide the keyword
DEFAULT
; e.g.INSERT INTO test (id, timestamp_created) VALUES (DEFAULT, DEFAULT) RETURNING *;
Looking at docs, it appears the (only?) way to handle this is to specify a transformation for undefined values, but I cannot get it to work as expected.
The following is a simplified representation of the problematic config.
Given tables created as follows:
... and a
postgres
connection helper created like so:... and insert functions defined as such:
... calling the helpers will yield the following results:
We're listing the columns in our insert helper because omitting the property that has a default value and letting postgres.js implicitly handle this causes a different error:
Error [PostgresError]: null value in column "archived" of relation "webpage" violates not-null constraint
Is this a bug? Or is there a better way to achieve what we want?