Simplify: Move code into database functions | Hugonweb Annotated Link Bibliography
Keep as much logic in the database, using constraints, stored procedures, and functions (Postgres even lets you write them in Python) as possible.
This reduces duplication of logic between the DB and external code, and allows the external code to change without issue.