A common data consistency issue is writing empty strings to the database. An empty string "" is a perfectly valid text or varchar but is usually not what you want.
Thankfully, Postgres has a way of checking for empty strings that prevent storing bad data. A domain is a reusable way to check a value that looks like this:
create domain content as text check (value != '');
This can be used as the column type when creating a table:
create table posts (
id serial primary key,
title content not null,
);
Now you’ll get a friendly error if you try to save an empty string. If you are using SQLAlchemy, you can specify a database model using a domain too.
Read Check data easily with PostgreSQL domains by Benjamin Sago.
See also:
- Postgres is the most exciting boring technology because of features like this
- Most business problems are data modeling problems