"DO UPDATE SET" not working as expected

Gargoyle

I'm using the PDO library in PHP and I'm generating a PostgreSQL statement like this:

INSERT INTO production_work (order_id, producer_id, sw)
VALUES (?,?,?)
ON CONFLICT (order_id, producer_id, day)
DO UPDATE SET sw = EXCLUDED.sw + ?
RETURNING ident

and for the parameters to the PDO execute() statement I'm passing in the array [7172, 10, 1, 1].

That production_work table has a default value for the day parameter of now() which is why I don't pass it in, and a unique constraint set on the three fields listed for the ON CONFLICT part.

When there is no conflict, it's correctly setting the sw column's value to 1. However, when there is a conflict, and the value of that sw column is currently 0, then after this statement runs the sw column ends up with a value of 2, not 1.

What am I doing wrong?

klin

The record EXCLUDED contains values which were to be inserted but caused the conflict, so EXCLUDED.sw = 1 in this case. There is no need to add to it another 1:

INSERT INTO production_work (order_id, producer_id, sw)
VALUES (?,?,?)
ON CONFLICT (order_id, producer_id, day)
DO UPDATE SET sw = EXCLUDED.sw
RETURNING ident;

If you want to add a given value to the existing one use:

INSERT INTO production_work (order_id, producer_id, sw)
VALUES (?,?,?)
ON CONFLICT (order_id, producer_id, day)
DO UPDATE SET sw = sw + EXCLUDED.sw
RETURNING ident;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"NOT IN" not working as expected

From Dev

Postgres trigger interfering with 'on conflict do update set'

From Dev

AutoResizeTextView Not Working as Expected

From Dev

PHP mktime not working as expected

From Dev

stopPropagation of jquery not working as expected

From Dev

laravel middleware not working as expected

From Dev

malloc() in C not working as expected

From Dev

__construct function not working as expected

From Dev

Javascript substring not working as expected

From Dev

bootstrap grid not working as expected

From Dev

substr() is not working as expected

From Dev

Bash IF not working as expected

From Dev

Symbol visibility not working as expected

From Dev

EJB @Lock not working as expected

From Dev

Polymorphism not working as expected in scala

From Dev

Javascript classList is not working as expected

From Dev

Keras ImageDataGenerator not working as expected

From Dev

Local Storage is not working as expected

From Dev

OnPropertyChanged not working as expected with ObjectListView

From Dev

multithreading in python not working as expected

From Dev

Purecss grids not working as expected

From Dev

didHighlightItemAtIndexPath not working as expected

From Dev

NgFor and JQuery not working as expected

From Dev

Sort List<> not working as expected

From Dev

Pipes not working as expected on Mac

From Dev

Regex formatting not working as expected

From Dev

Output buffering not working as expected?

From Dev

getElementById is not working as expected?

From Dev

Webpack CommonsChunkPlugin not working as expected