Database Schema Design Mistake That Broke My App

From As The Geek Learns · 10:24

Show by The Geek

TL;DR: My Johnny Decimal index app had a three-level database schema when the system requires four levels. I’d conflated folder containers with the files inside them—a fundamental data model flaw. One week of dogfooding caught what months of planning completely missed. Key lessons: use your own software early, match your data model to your mental model, and refactor before you accumulate data you’ll need to migrate.I’d been using JDex, my Johnny Decimal index application, for about a week when a fundamental database schema design flaw became impossible to ignore.The app worked beautifully. I could create areas and categories. I could track items with their XX.XX identifiers. The search was fast, the UI was clean, and I was feeling pretty good about what I’d built.Then I tried to organize real files.Somehow I had folders and the files inside them sitting at the same level in my database. My data model had three levels when Johnny Decimal requires four.Here’s how one week of dogfooding caught what months of planning completely missed and the four lessons I’m carrying into every future project.The Problem I Didn’t See ComingHow Johnny Decimal Hierarchy Actually WorksJohnny Decimal (johnnydecimal.com) is a system for organizing files and information. It has a clear four-level hierarchy:Area (00-09)└── Category (00, 01, 02...)└── ID Folder (00.01, 00.02...)└── Actual FilesLevel What It Is Number Format Example1 Area 00 - 09 00-09 System2 Category XX 00 Index3 Folder XX.XX 00.01 JDex Database4 Item XX.XX.XX 00.01.01 Main DB FileMy database had three levels: areas, categories, and... items.Where do the ID folders go?Why My Three-Level Database Schema FailedI’d conflated two fundamentally different things:Folder containers: the XX.XX numbered folders that hold filesActual files: the contents inside those foldersIn my original schema design, “00.01 JDex Database” (a folder) and the SQLite file inside it (an item) existed at the same level in the database. That’s like saying a filing cabinet drawer and the documents inside it are the same thing.Why Database Schema Mismatches Break Core FunctionalityThis wasn’t just aesthetically wrong. The three-level data model broke features I needed:I couldn’t track what was inside a folder. The whole point of Johnny Decimal is knowing “everything about taxes lives in 12.04.” But my schema had no way to list what was actually in 12.04. The folder and its contents were peers, not parent and child.Folder-level properties were impossible. What if everything in a folder should be marked as sensitive? With my original design, I’d have to set that flag on every individual item. There was no way to say, “This container and everything in it shares this property.”The numbering didn’t work. Johnny Decimal items should be XX.XX.XX—three segments. Mine only went to XX.XX because I’d eliminated an entire level.When your data model doesn’t match the mental model of the system you’re building, you’ll keep running into walls. The database fights you instead of helping you.The Fix: Adding a Fourth LevelThe solution was straightforward conceptually but required rethinking the entire database schema: the old items table became folders. A new items table was created for the actual tracked objects inside those folders.The Refactored Database SchemaHere’s what the corrected structure looks like:-- Folders (Level 3) - XX.XX format containersCREATE TABLE folders (id INTEGER PRIMARY KEY,folder_number TEXT UNIQUE, -- “22.05”category_id INTEGER,name TEXT,sensitivity TEXT DEFAULT ‘standard’,created_at TIMESTAMP,updated_at TIMESTAMP,FOREIGN KEY (category_id) REFERENCES categories(id));-- Items (Level 4) - XX.XX.XX format objectsCREATE TABLE items (id INTEGER PRIMARY KEY,item_number TEXT UNIQUE, -- “22.05.03”folder_id INTEGER,name TEXT,sensitivity TEXT DEFAULT ‘inherit’, -- Can inherit from folderfile_path TEXT,notes TEXT,created_at TIMESTAMP,updated_at TIMESTAMP,FOREIGN KEY (folder_id) REFERENCES folders(id));The inherit option for item sensitivity was a bonus insight that emerged from the refactor. Most items should just use their folder’s sensitivity level. Only exceptions need explicit overrides. That’s not something I could have implemented with the old three-level schema.Four Lessons from This Database Design MistakeUse Your Own Software (Dogfooding)The schema flaw was invisible in design documents and code reviews. It only appeared when I tried to organize real files with real data.A week of daily use caught what months of planning missed.There’s no substitute for using your own software to accomplish real tasks. You’ll discover conceptual mismatches that no amount of testing or review will reveal. If you’re building something, use it yourself as soon as it’s minimally functional.Match Data Models to Mental ModelsI knew Johnny Decimal had four levels. I could draw them on a whiteboard. Yet somehow my database had three.That mismatch was always going to cause problems—I just hadn’t encountered them…

More episodes of As The Geek Learns

More episodes

PodSearch is a free, independent podcast search engine. Read about PodSearch, the privacy policy or contact us.