PostgreSQL multi-row updates in Node.js

前端 未结 2 1547
梦如初夏
梦如初夏 2020-11-29 12:30

As i already found here On Stackoverflow it is possible to update multiple rows in one query by doing something like this

update test as t set
    column_a =         


        
2条回答
  •  我在风中等你
    2020-11-29 13:15

    First of all, I did accept the answer from @vitaly-t, as he taught me to use a better and faster library pg-promise and it did solve my problem. (period)

    But to answer my own question for the people who could end up with the same problem and want to stay with the library pg, here is where i made the mistake (it's just syntax)

    In my original code i had this line at the end

    return {
        text: 'UPDATE fit_ratios as f set ratio_budget = c.ratio_budget from (VALUES ' +  chunks.join(', ') + ') as c(ratio_label, ratio_budget) WHERE c.ratio_label = f.ratio_label', values: params
            }
    }
    

    The first time i saw this it was hard to understand so it was very easy to make some mistakes. changing this line of code to what you see underneath fixed my original problem to

    return {
        text: 'UPDATE fit_ratios as f set ratio_budget = c.value from (VALUES ' +  chunks.join(', ') + ') as c(id, value) WHERE c.id = f.ratio_id',
                    values: params
    }
    

    I just missed by using column names instead of using the keys of my object. (e.g. c.ratio_label to c.id, ...)

提交回复
热议问题