The Versatile Check Constraint

If constraints can be considered “cool,” then the check constraint[85] has to be the coolest. This is a flexible type of constraint you’ll use in multiple ways.

Any condition that you can express using SQL that evaluates to a Boolean result can be written for a check constraint. Create a Boolean condition rule that’s checked any time data is inserted, updated, or deleted.

Like other types you’ve seen so far, check constraints are added to a table.

For Rails developers, you’ll want to know that support for check constraints was added to Active Record in version 6.1,[86] and support for if_exists was added later.[87] This means helper methods can be used from migrations to create check constraints.

Check constraints ...

Get High Performance PostgreSQL for Rails now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.