node.js - Issue in loop update of rows of DB - Stack Overflow

I was setting up foreign key of one table by using some data from other table. I am trying to do this b

I was setting up foreign key of one table by using some data from other table. I am trying to do this by using for loop in node-js and the db is in postgres. There is too much data and I am not sure why all rows are updated, some are left null at the end even tough I am updating then one row at a time. I am using drizzle orm to access the db and node-postgres client.

Example Tables -

Table A - code (varchar)(PK), api_response (json), reference_to_table_b 
Table B - name (varchar)(PK)   

Now my data in table A is populated other than FK to table B. To set this foreign key I have to check some fields in api_response, then set the foreign key according to condition.

To do so I am using a loop in node.js as -

const allRows = await db.select().from(table_A);

for(let aInstance of allRows) {
   if(aInstance.api_response.fields === condition) {
       const correspondingRowInB = await db.select().from(table_B).where({condition});

       const updatedRow = await db.update(table_A).set({ updated_field }).where({ update_condition });
   }
}

Some of the things I suspected were -

1.) It may be because of async access so I have added async await to ensure that it proceeds ahead only after update is done. But still it does not work.

2.) dirty write is happening hence old value is reflected which is null. But when I wrap my looped updates in transaction with async await still the same output is seen.

Can someone guide what could be the issue?

Can someone also guide about why such loop updates are antipatterns and not good to use?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745559018a4632992.html

相关推荐

  • node.js - Issue in loop update of rows of DB - Stack Overflow

    I was setting up foreign key of one table by using some data from other table. I am trying to do this b

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信