I am trying to update a row in pgsql table using pg-promise helpers. The query executes successfully and returns success return code. But the changes are not reflecting in the table. Should I issue COMMIT after update query or will it commit automatically in general?
const condition = pgp.as.format(` WHERE key = '${value}'`, putData);
const table = new pgp.helpers.TableName({ table: 'mytab', schema: 'myschema'});
const query = pgp.helpers.update(putData, [updateCols], table) + condition;
await db.none(query).then(() => {
status = 200;
response['status'] = 'success';
})
.catch(error => {
status = 500;
response['status'] = 'failed';
response['error'] = error.message;
response['errorCode'] = error.code;
});
The reason it is not updating the corresponding record is invalid input in where condition. Spaces/Null passed into WHERE condition. So it returns success without updating anything. Now the code has been corrected and it is working fine.
Secondly (it is not related to the question), I was using ES6 string format in my query formatter. As per @vitaly-t's comment, I changed it as below.
pgp.as.format('WHERE key = $1', [keyValue]);
or
pgp.as.format('WHERE key = ${keyValue}', {keyValue});