问题
I have a table available_trucks
. I need to update the available_date
column inside at some specific time. So first i am getting the data from the table , i am changing like i should the available_date inside, and try to update available_date on every row in db.
But it is not working using knex.js when i try for multiply values in my for loop
async function updateAvailableTrucks(availableTrucks) {
for(let i = 0;i < availableTrucks.length;i++) {
await db("available_trucks").update('available_date', availableTrucks[i].available_date).where('id', availableTrucks[i].id);
}
}
if i try just for one value then it will work for example on one iteration with hardcoded value
for(let i = 0;i < 10;i++) {
if(i == 4) {
await db("available_trucks").update('available_date', '2020-12-12T04:47:56.126').where('truck_number', 228);
}
}
i saw this link
Batch update in knex
and i tried the solution from there too
batchUpdate({ table: 'available_trucks', column: 'available_date' }, availableTrucks);
function batchUpdate(table, collection) {
return db.transaction(trx => {
const queries = collection.map(tuple =>
db('available_trucks')
.where('id', tuple.id)
.update({available_date: tuple.available_date})
.transacting(trx)
);
return Promise.all(queries)
.then(trx.commit)
.catch(trx.rollback);
});
}
but the data inside DB is still not changed
来源:https://stackoverflow.com/questions/65390819/how-can-i-update-every-row-in-my-table-using-knex-js