I hesitated for an hour before typing ALTER TABLE users ADD COLUMN is_archived BOOLEAN. The failures came quietly: the ORM defaulted it to None, the serializer dropped it, the TypeScript build fell over.
The column took five minutes. Convincing the ORM, the serializer and the frontend that they were all talking about the same column took two days.
Here is the same feature twice.
The ferryman version
# BEFORE — everyone does a bit of everything
def archive_user(user_id: str):
row = db.execute("SELECT * FROM users WHERE id = %s", (user_id,)).fetchone()
if row is None:
return None # the API layer will turn this into 404
db.execute("UPDATE users SET is_archived = true WHERE id = %s", (user_id,))
return dict(row) | {"is_archived": True} # the table, wearing a JSON hat
@app.post("/users/{user_id}/archive")
def archive(user_id: str):
user = archive_user(user_id)
if user is None:
raise HTTPException(404)
return user # the shape of the DB leaks to the client
Three decisions are hidden in nine lines: what "archived" means (a true with no timestamp), what "missing" means (Python's None), and what the client is promised (the entire row). Change one and you find out what breaks in production.
The boundary version
The database defends the data:
-- no user is archived without a timestamp
ALTER TABLE users ADD COLUMN archived_at timestamptz;
ALTER TABLE users ADD CONSTRAINT archived_needs_time
CHECK (is_archived = (archived_at IS NOT NULL));
Python defends the domain:
def archive(user_id: UserId) -> User:
user = repo.get(user_id) # the domain decides what "missing" means
if user is None:
raise UserNotFound(user_id)
user.archive() # the rule lives here, once
repo.save(user)
return user
The API defends the contract:
class UserArchived(BaseModel): # not the table, a promise
id: str
archived_at: datetime
The difference: a CHECK constraint rejects a half-archived row, and response_model strips anything the client wasn't promised. The machine enforces the boundary, not discipline.
The three boundaries
-
Postgres owns the truth of the data: types, constraints, relations. If Python is validating what a
CHECKcould reject, you're writing the same rule twice. - Python owns the truth of the domain: rules, flows, decisions. If your Python collapses into SELECT → dict → transform → dict → INSERT, you have replaced a JOIN with a loop.
- The APIs own the truth of the contract: what you promise, what you accept, what you hide. If your JSON mirrors the table, every refactor is a breaking change.
How to tell you need this
- You hesitate before adding a column.
- Nobody can say where a business rule lives, because it lives in all three.
- Every change touches five files to do one thing.
Two out of three, and it's no longer a smell. It's architecture telling you a boundary is missing.
Where to start
You don't rewrite the system. You add one boundary at a time, starting where the pain is loudest.
- Find the rule written twice. Grep for the same validation in SQL, in Python, in the API layer. Pick one home — usually the constraint — and delete the copies.
-
Name the rule. If a business rule lives inside a loop as an
if, move it into a method with a name (is_overdue,archive) and give it a test. The name is the boundary. -
Declare the contract. Add
response_model(or its equivalent in your framework) to every endpoint that returns a row. Five lines, and an entire class of "we didn't know the client read that field" disappears.
Start with the column that scares you. If the change stops surprising you afterwards, you have found the thing.
What it costs
This costs you something: a constraint to write, a method to name, a model to declare. In exchange, the invisible ripple effects become visible — and then they stop existing. The fragility you feel is not in the problem you are solving. It's in the three separate truths you maintain by hand.
Stop being a ferryman. Become the boundary.
Published on blog — comments and corrections welcome.
Top comments (0)