Despite some exciting advances in the field, like Node, Redis, and Go, a well-structured relational database fronted by a Rails or Sinatra (or Django, etc.) app is still one of the most effective toolsets for building things for the web. In the coming weeks, I’ll be publishing a series of posts about how to be sure that you’re taking advantage of all your RDBMS has to offer.
ASSUMING MY LAST POST CONVINCED YOU of the why of marking required fields
NOT NULL, the next question is how. When creating a brand new table, it’s straightforward enough:
CREATE TABLE employees ( id integer NOT NULL, name character varying(255) NOT NULL, created_at timestamp without time zone, ... );
When adding a column to an existing table, things get dicier. If there are already rows in the table, what should the database do when confronted with a new column that 1) cannot be null and 2) has no default value? Ideally, the database would allow you to add the column if there is no existing data, and throw an error if there is. As we’ll see, depending on your choice of database platform, this isn’t always the case.