Upsert for sqlalchemy
I've been working on a codebase that included someone's implementation of an upsert
function for sqlalchemy. It's a problem that you can approach a bunch of different ways, including at the sql layer, but a prior engineer had written a method in a mixin class that boils down to:
existing_val = getattr(existing, column.name, None) new_val = getattr(self, column.name, None) if new_val != existing_val: setattr(existing, column.name, new_val)