I was cropping a dashboard screenshot for a post when a dropdown entry stopped me: Shop 5. No phone number, no orders, created this morning. I had been telling myself all week that this screen only ever showed real customers, and I had a green check to back that up. One SELECT later, Shop 5 turned out to be mine — not imported, not seeded, minted by a health check that asked whether it existed.
The route itself is the most ordinary thing in that codebase. /factory/<id> reads a db_file out of a small registry table, opens it, renders the order list. Nothing smells. Then I opened factory_db_path() and read it for the first time in weeks:
def factory_db_path(fid): fid = int(fid or 1) conn = reg() row = conn.execute('SELECT db_file FROM factories WHERE id=?', (fid,)).fetchone() if not row: db_file = 'orders.db' if fid == 1 else f'factory_{fid}.db' conn.execute('INSERT INTO factories (id,name,phone,db_file,created_at) VALUES (?,?,?,?,?)', (fid, f'Shop {fid}', '', db_file, _now())) conn.commit() else: db_file = row['db_file'] conn.close() path = os.path.join(DATA_DIR, db_file) if not os.path.exists(path): init_factory_db(path) seed_templates(fid, path) return path A function whose name promises a path performs three writes: a registry row, a database file, and eight stage templates. Every view that takes a factory id calls it, including the ones that only read.
And the quiet part is two lines up in init_db(), which ends with seed_templates(1, factory_db_path(1)). Boot touches the resolver before anything else, so id 1 always exists in the registry and id 9999 never has to fail — it gets a fresh empty tenant, the default workflow stages, a 200, and an order list with zero rows. It gets counted in total_shops forever.
I was wrong about where it came from, and I was wrong twice.
First I assumed leftover demo data, because the fixture in that repo seeds a couple of shops and I had pasted those exact names into a screenshot the week before. I wrote the responsible little script that deletes factories with an empty phone number, ran it, watched the count drop, felt clever. Next morning there were two new ones.
Then I was convinced the test suite was doing it. The same names reappeared right after a pytest run, which felt like a smoking gun. I commented out the fixture, ran the suite, and a Shop 17 showed up that no test had ever mentioned. That was the twelve minutes where I stopped trusting my own grep and did the boring thing: grep -n "factory_db_path" app.py and read every single call site. Almost all of them are read views. One of them is a create route. The resolver was doing the creating for both.
My green check passed for the wrong reason, which is the part that still annoys me. It hit /factory/1 and asserted the page rendered. Id 1 exists in every deployment because boot put it there, so that probe could never see the bug. It was measuring my boot path, not my read path. Silly mistake, and it survived a week of me feeling pleased about it.
The fix is boring on purpose. Two functions instead of one:
def factory_db_path(fid): # read-only: returns None when unknown row = reg().execute(...).fetchone() return os.path.join(DATA_DIR, row['db_file']) if row else None def create_factory(name, phone=''): # the only place that INSERTs Views resolve, get None, and 404. Startup creates the default tenant explicitly through create_factory, so boot no longer needs a resolver with a side effect to exist. The diff is small — app.py +8/−3 — and the test file that came with it is +57, because a 3-line fix needs a test that fails first. test_unknown_factory_id_creates_nothing asserts the registry count is unchanged and that no factory_424242.db lands in DATA_DIR. Before the change it failed on both assertions. After it, the full suite is 98 passed, 2 xfailed; the two xfails are pre-existing and unrelated.
A lookup that writes is not a lookup, and the name is a lie you will eventually believe. get_ and ensure_ are two different verbs with two different failure modes, and merging them means every typo in a URL is a silent insert. I would rather 404 a customer who deserves a 200 than hand a stranger an empty tenant that shows up in my shop count, my billing alerts and my screenshots.
I've only measured this on my own Flask + SQLite setup, so treat the sample size as one.
My question for the room: on Flask 3.x with SQLite and a request handler that resolves /<id> before the view body runs, how many of your read endpoints would still insert a row if I hit them with 424242? Mine was one, and it took a screenshot to find it.
If you think a resolver with a side effect is a reasonable trade for shorter route code, I'd argue you are one typo away from a tenant — I want the case where it actually saved you, so comments are open.
