is it possible in SQLAlchemy to enforce maximum string length of value assigned to mapped column? All I want is to raise an exception if an assigned string value is longer then the length of the corresponding table column of type STRING.
Thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s easiest to just rename the mapped column and proxy it through a property:
You can easily factor out the property creation, and even use a generic validation framework like formencode.
If you need a more SQLAlchemy specific solution and don’t mind using specific interfaces, then SQLAlchemy has an extension mechanism for capturing events on attributes. A validator using that would look something like this:
You would then use this extension by setting
__sa_instrumentation_manager__ = InstallValidatorListenerson any class you want validated. You can also just set it on the Base class if you want it to apply to all classes derived from it.