no-missing-foreign-key

Warn when including relations without a foreign key

Severity: Warning

This is a schema-aware rule. It requires a [database] section in your config pointing to a Prisma schema file.

What it does

Flags .include() calls in Drizzle queries where the included relation does not have a corresponding foreign key defined in the database schema.

Why it matters

An include without a foreign key suggests a mismatch between your code and your schema. The join may still succeed (e.g., if columns happen to match), but the relationship is not enforced at the database level, risking data integrity issues.

How it works

The rule finds the table being queried and checks whether any column in that table has a foreign key defined. If the table has no foreign keys at all, any .include() call on that table is flagged.

When to disable

Disable this rule if your schema uses implicit relationships or if the foreign key is managed outside Prisma (e.g., via raw migrations or a separate schema management tool).

Examples

Bad
// `posts` table has no FK on `authorId` referencing `users`
const result = await db.query.posts.findMany({
  include: { author: true }, 
});
Good
// `posts.authorId` has an `@relation` pointing to `users.id`
const result = await db.query.posts.findMany({
  include: { author: true }, 
});

Last updated on

On this page