Skip to content

Column Types

Thomas Pollet edited this page Apr 27, 2021 · 4 revisions

Using SQLAlchemy TypeDecorator subclasses it is possible to create custom columns, for example:

    email = db.Column(EmailType)
class EmailType(TypeDecorator):
    """
        example class to perform email validation
        DB Email Type class: validates email address when bound
    """

    impl = db.String(767)

    def __init__(self, *args, **kwargs):
        super(*args, **kwargs)

    def process_bind_param(self, value, dialect):
        if "@" not in value:
            raise ValidationError("Email Validation Error {}".format(value))

        return value

Example

See also: https://docs.sqlalchemy.org/en/13/core/custom_types.html