Skip to content

⭐ Models and Migrations

Terrence Daniels edited this page Aug 14, 2026 · 1 revision

4 models (User, Article, Comment, Tag) and their matching migrations, added one file at a time with cross-checked associations. Two real bugs came out of this, both invisible until the app stopped auto-patching its own schema at boot.

User.js's Comments association used the wrong foreign keyarticleId, a copy-paste leftover from Article.js's own Comments association — when Comment.js actually declares its author relation as foreignKey: "userId". Fixed, and caught for real: the fix was deliberately reverted and the association test watched to confirm it fails before being restored, the same proof-of-meaningfulness standard applied to every fix in this repo.

Both create-article and create-comment migrations never created the userId/articleId foreign-key columns their own models' associations need. This only ever worked in the source app because backend/index.js called sequelize.sync({ alter: true }) on every boot, silently patching the live schema to match the models — meaning the migrations were never actually the schema source of truth. Fixed by adding the real FK columns (matching each model's own cascade behavior); sync({ alter: true }) itself was removed later, once these migrations became genuinely correct (see TypeScript Migration's index.ts phase). Every migration test now cross-checks its column shape against the real model to catch this class of drift going forward.

Clone this wiki locally