O'Reilly logo

Rails, Angular, Postgres, and Bootstrap by David B. Copeland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Prevent Bad Data Using Check Constraints

If you’ve done any database work at all, you’re no doubt familiar with a “not null” constraint that prevents inserting null into a column of the database.

 CREATE​ ​TABLE​ people (
  id ​INT​ ​NOT​ ​NULL​,
 name​ ​VARCHAR​(255) ​NOT​ ​NULL​,
  birthdate ​DATE​ ​NULL
 );

In this table, id and name may not be NULL, but birthdate may be. Postgres takes the “null constraint” concept much further by allowing arbitrary constraints on fields. Postgres also has support for regular expressions. This means we can create a constraint on our email field that requires its value to match the same regular expression we used in our Rails code. This would prevent non-company email addresses from being inserted ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required