shlogg · Early preview
Chevy Vall @gutmaster

Validating Database Attributes With SQLAlchemy

Validating database attributes with SQLAlchemy is a breeze thanks to the `@validates` decorator, ensuring only correct data reaches your code.

Validations are a method of ensuring that our databases only receive the type of information that is appropriate for each attribute. After all, we wouldn't want a surprise type of data to find it's way into our code and cause unexpected behavior. Fortunately, SQLAlchemy has a package that makes validations quick and easy! 
Let's look at some simple examples. Imagine we have a simple model, Sandwich. Here we have already initialized our database and are importing it from a configuration file.

from config import db
class Sandwich(db.Model):
    __tablename__ = 'sandwiches'
    id = db.Column(db...