kgz.me - Klaus Zanders

Thoughts of a random guy on the internet. Doing things with software ... or video

PostgreSQL - enforce only one column filled via constraint

2023-08-08 3 min read Code-Snippets
Recently I got a task, that required me to ensure that of two columns, either none, or only one get filled. That should be ensured on the database level. In my #ruby code it would be easy to add a validation to check for this, but as we are doing manual inserts into the DB, we need to also make sure, that the database enforces this constraint. Luckily, postgres has a feature for us, that was introduced in version 9. Continue reading