Conditional insert using SQLAlchemy?

Viewed 19

I've got a fairly simple DB model called Payout with an amount_cents attribute. I'd like to insert a new Payout only when the amount_cents is less than the sum of another model called Payment, the query for a Payment can be expressed as follows

        self.session.query(
            func.sum(Payment.amount_cents) > payout.amount_cents
        )

is there a way to conditionally fail an entire transaction in SQLAlchemy if that query comes back as false? Currently I'm inserting using the following syntax

   self.session.add(payout)
   self.session.commit()
0 Answers
Related