How to update multiple records with incrementing some field (in my case id
)?
I missed one record and the whole table shifted.
How to do that in one transaction or one query? Or what is the fastest way to do that?
Tried something like below, but it is too slow.
rows = session.query(Table).all()
for row in rows:
row.id = row.id + 1
session.commit()
Also tried to use something like, but it's not working for me:
session.query(Table)\
.filter(Table.id == row.id)\
.update({'id': row.id + 1})
Example of data I have:
ID Value
1 A
2 B
3 C
< -- D is missing here
4 E
5 F
Here, if it would be alphabet D
should have ID=4, then I need to increment E
and F
.
It is not something complicated when you have 5 records, but problem occurs when you have millions or billions of records with this issue.