Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple validation with SQLAlchemy

I'm new to sqlalchemy, and I'm trying to achieve simple validation of model's fields, as provided by Django ORM (min & max for Integer, email, ...). Can SQLAlchemy do this sort of field validations out of the box ? By the way, I'm using SQLAlchemy with Flask.

like image 859
arnaud briche Avatar asked Oct 29 '25 06:10

arnaud briche


1 Answers

See Simple Validators in the documentation. Sample code extract below:

class EmailAddress(Base):
    __tablename__ = 'address'

    id = Column(Integer, primary_key=True)
    email = Column(String)

    @validates('email')
    def validate_email(self, key, address):
        assert '@' in address
        return address
like image 166
van Avatar answered Oct 31 '25 02:10

van



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!