Whenever possible make fields not null from day 1

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the databases category.

Last Updated: 2022-12-01

Imagine the users table has a phone_number column that starts off with no constraints - in particular, it can be null or not null.

Later you decide that EVERY user must have a phone number. But, given that your code has been live with this nullable attribute for a while, many of the existing user rows won't be able to satisfy this requirement.

This dooms your code to be complicated forever, because there is likely no way to backfill the data since these users may not respond to your requests for the data. Either you must decide to do without the DB constraint forever, or you add a fake phone number for the ones with nulls and handle this in your application code with a dirty "if statement" sure to trip up future maintainers.


Whenever possible, make DB fields NOT NULL from the get-go.