0

I’m trying to validate a SQLAlchemy model before it is inserted or updated, e.g
class MyModel(db.Model):
foo = db.Column(db.String(255))
bar = db.Column(db.String(255))

I’ve tried a few approaches, but none seem to work. One possibility was to listen to before_insert and before_update events, e.g
@event.listens_for(MyModel, ‘before_insert’)
@event.listens_for(MyModel, ‘before_update’)
def validate_my_model(mapper, connection, model):
if not is_valid(model):
raise Exception(“the model isn’t valid”)

This works okay, but in tests I get this error unless I roll back the session.
This Session’s transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback()

I could call session.rollback() in the tests, but I that seems incorrect, since the test is just issuing PUT/POST requests and shouldn’t really know anything about the session or any SQLAlchemy internals.
Another option was to listen to init events, instead of before_insert and before_update, but that doesn’t handle the update case.
I also tried using the @validates, e.g
@validates(‘foo’, ‘bar’)
def validate(self, key, val):

However, the validation relies on both properties, while the validate method is called individually for each property. This means that if I try to set both foo and bar on the model, it attempts to validate the intermediate state where one property has been set but the other hasn’t.

Kuldeep Baberwal Changed status to publish February 17, 2025