How ORM Migrations Are Hiding Missing Indexes (And How to Actually See Your Database)
I spent 4 hours last week debugging a slow API endpoint. The culprit? A missing index on a foreign key that my ORM "helpfully" abstracted away during a schema migration. When you use modern tools l...
Source: DEV Community
I spent 4 hours last week debugging a slow API endpoint. The culprit? A missing index on a foreign key that my ORM "helpfully" abstracted away during a schema migration. When you use modern tools like Prisma or Drizzle, it’s incredibly easy to forget what your database actually looks like. The visual connection between tables is lost in hundreds of lines of TypeScript. 🛑 The Hidden Danger of ORMs Let's look at a standard relational setup: you have Users and Orders. In your ORM, you define a relation. The ORM generates a migration adding a user_id column to Orders with a foreign key constraint. What it doesn't always do automatically is add a B-Tree index to user_id. -- What the ORM generated ALTER TABLE "orders" ADD CONSTRAINT "fk_user" FOREIGN KEY ("user_id") REFERENCES "users"("id"); -- What you actually needed for performance CREATE INDEX "idx_orders_user_id" ON "orders"("user_id"); Without that index, every time you query a user's orders, Postgres has to perform a sequential scan