Using constraints is considered a good practice. This way, you can restrict the domain of a certain model attribute and ensure data integrity and quality. There are many types of constraints that you can use; primary key and foreign key constraints were already covered in the previous sections. The other kinds of constraints that are supported by SQLAlchemy are shown in the following list:
- Not NULL (ensures that a certain attribute contains data)
- UNIQUE (ensures that a certain attribute value is always unique in the database table, which contains the model data)
- DEFAULT (sets a default value for the attribute when no values were provided)
- CHECK (used to specify range of values)
Using SQLAlchemy, you can ensure that your data's domain restrictions are explicit and all in the same place, not spread across your application code.
Let's improve...