book = Book.get(isbn="978-0099466031")
book.copies_in_stock -= 1
book.save() # WRONG! irreparable data loss here
is that in SQL you should write it like this: update books set
copies_in_stock = copies_in_stock - 1
where isbn = '978-0099466031';
No needs of transactions.I don't remember how to do it with Django's ORM but in Rail's ActiveRecord I'd use directly the connection object
update_query = sanitize_sql(['update books set copies_in_stock = copies_in_stock - 1 where isbn = ?', isbn])
ActiveRecord::Base.connection.execute(update_query)
This saves a lot of programming pain, possibly skip some unnecessary bugs and get better performances.sanitize_sql is from https://api.rubyonrails.org/classes/ActiveRecord/Sanitizatio...