How can I verify Column data types in the SQLAlchemy ORM?

后端 未结 2 588
一生所求
一生所求 2020-11-28 08:54

Using the SQLAlchemy ORM, I want to make sure values are the right type for their columns.

For example, say I have an Integer column. I try to insert the value “hell

2条回答
  •  -上瘾入骨i
    2020-11-28 09:17

    Improving on the answer of @zzzeek , I suggest the following solution:

    from sqlalchemy import String
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.event import listen_for
    
    Base = declarative_base()
    
    @listens_for(Base, 'attribute_instrument')
    def configure_listener(table_cls, attr, col_inst):
        if not hasattr(col_inst.property, 'columns'):
            return
        validator = getattr(col_inst.property.columns[0].type, 'validator', None)
        if validator:
            # Only decorate columns, that need to be decorated
            @listens_for(col_inst, "set", retval=True)
            def set_(instance, value, oldvalue, initiator):
                return validator(value)
    

    That lets you do things like:

    class Name(String):
        def validator(self, name):
            if isinstance(name, str):
                return name.upper()
            raise TypeError("name must be a string")
    

    This has two benefits: Firstly, there is only an event triggered, when there actually is a validator attached to the data field object. It does not waste precious CPU cycles on set events for objects, that have no function for validation defined. Secondly, it allows you to define your own field types and just add a validator method there, so not all things that you want to store as Integer etc run through the same checks, just the ones derived from your new field type.

提交回复
热议问题