0

This is the query I pass into a batch transaction:

INSERT INTO table VALUES(${id}, ${name}, ${crtd});

The input array may or may not contain one of the keys, say ${crtd}. This throws Error: property 'crtd' does not exist and the entire batch fails.

I still want this row to be inserted, containing only {id} and {name}. The ${crtd} is a nullable column too.

In the below, 'l' is the input json missing the key ${crtd}.

                           db.tx(t=>t.batch(valuesArray.map(l=>t.none(query, l))))                         So, the only way to do this is additional logic that checks for missing keys and adds them?

How to handle this?

Harini
  • 41
  • 4
  • Why not just assign a default value to the formatting object before running the query? – vitaly-t Aug 22 '16 at 11:26
  • Currently I have no formatting logic in place and pulling the input JSON array from a WebService. Did not want to add additional logic, if I could get the transaction to ignore missing Keys in each input json object. Missing values is working allright, but missing keys is the problem, because ${crtd} refers to a key in the input json. So, the only way to do this is additional logic that checks for missing keys and adds them? – Harini Aug 22 '16 at 12:40
  • Edited the original question to include the Tx described by you in another thread. – Harini Aug 22 '16 at 12:57
  • If you talking about inserting records from JSON, you should look at [helpers.insert](http://vitaly-t.github.io/pg-promise/helpers.html#.insert), you can set default/init values via `ColumnSet` ;) – vitaly-t Aug 22 '16 at 13:49
  • Much thanks for ur quick response @vitaly-t! I will check it. – Harini Aug 22 '16 at 17:07

0 Answers0