11 ms·
No, with transaction.atomic(): for user in db.users.all(): user.do() is not the same as for user in db.users.all(): with transaction.atomic
by zepolen 8y ago
No,
with transaction.atomic():
for user in db.users.all():
user.do()
is not the same as
for user in db.users.all():
with transaction.atomic():
user.do()
If the first fails, the entire data rolls back, if the second fails, half your db might be in an inconsistent state.
- theptip 8y agoYes, of course, they are not the same. I did not claim they were. I said: > You can often recast that migration to something more like By which I mean, in my experience you can usually write your migrations so that your code can work with the old AND the new version of the data, in which case you don't need to have a transaction around the whole operation. This takes more work but is safer: https://martinfowler.com/bliki/ParallelChange.html https://martinfowler.com/bliki/ParallelChange.html https://www.amazon.com/Refactoring-Databases-Evolutionary-paperback-Addison-Wesley/dp/0321774515 https://www.amazon.com/Refactoring-Databases-Evolutionary-pa...