drizzle-team/drizzle-orm · error · Error

Table "${sourceTable[Table.Symbol.Name]}" not found in schem

Error message

Table "${sourceTable[Table.Symbol.Name]}" not found in schema

What it means

normalizeRelation() throws when the relation's source table (the table on which relations() was called) is not present in tableNamesMap. This means the table owning the relation was not registered in the schema config, so its relation cannot be normalized.

Source

Thrown at drizzle-orm/src/relations.ts:586

		};
	}

	const referencedTableTsName = tableNamesMap[getTableUniqueName(relation.referencedTable)];
	if (!referencedTableTsName) {
		throw new Error(
			`Table "${relation.referencedTable[Table.Symbol.Name]}" not found in schema`,
		);
	}

	const referencedTableConfig = schema[referencedTableTsName];
	if (!referencedTableConfig) {
		throw new Error(`Table "${referencedTableTsName}" not found in schema`);
	}

	const sourceTable = relation.sourceTable;
	const sourceTableTsName = tableNamesMap[getTableUniqueName(sourceTable)];
	if (!sourceTableTsName) {
		throw new Error(
			`Table "${sourceTable[Table.Symbol.Name]}" not found in schema`,
		);
	}

	const reverseRelations: Relation[] = [];
	for (
		const referencedTableRelation of Object.values(
			referencedTableConfig.relations,
		)
	) {
		if (
			(relation.relationName
				&& relation !== referencedTableRelation
				&& referencedTableRelation.relationName === relation.relationName)
			|| (!relation.relationName
				&& referencedTableRelation.referencedTable === relation.sourceTable)
		) {
			reverseRelations.push(referencedTableRelation);

View on GitHub (pinned to b7862528fd)

Solutions

  1. Include every source table that has a relations() definition in the schema object passed to drizzle().
  2. Consolidate schema into a single barrel export and pass it whole: import * as schema from './schema'; drizzle(client, { schema }).
  3. Resolve circular imports so source tables are defined before their relations are evaluated.

Example fix

// before (source table missing from schema)
const orderRelations = relations(orders, ({ one }) => ({
  customer: one(customers, { fields: [orders.customerId], references: [customers.id] }),
}));
const db = drizzle(client, { schema: { customers, orderRelations } }); // orders omitted

// after
const db = drizzle(client, { schema: { customers, orders, orderRelations } });
Defensive patterns

Strategy: validation

Validate before calling

// Validate that every table owning a relations() definition is in the schema.
import { Table } from 'drizzle-orm/table';
import { is } from 'drizzle-orm/entity';
import { Relations } from 'drizzle-orm/relations';

function validateSourceTables(schema: Record<string, any>) {
  const tableSet = new Set(Object.values(schema).filter((v) => is(v, Table)));
  for (const v of Object.values(schema)) {
    if (is(v, Relations)) {
      const src = (v as any).table;
      if (src && !tableSet.has(src)) {
        throw new Error(`Source table "${src[Table.Symbol.Name]}" missing from schema`);
      }
    }
  }
}

Type guard

import { entityKind } from 'drizzle-orm/entity';

function isRelations(v: unknown): boolean {
  return (v as any)?.[entityKind] === 'Relations';
}

Try / catch

try {
  await db.query.posts.findFirst();
} catch (e) {
  if (e instanceof Error && /not found in schema/.test(e.message)) {
    // add the missing source table to the schema export
  } else throw e;
}

Prevention

When it happens

Trigger: Calling relations(tableX, ...) and passing schema to drizzle() that does not include tableX. Defining relations in a separate file but forgetting to add the source table to the central schema export. Circular import leaving tableX undefined.

Common situations: Splitting relations into their own files and missing the source table in the aggregate schema. Refactoring that moves a table out of the schema index but leaves its relations file behind.

Related errors


AI-assisted analysis of drizzle-team/drizzle-orm@b7862528fd (2026-08-03). Data as JSON: /data/errors/79f23af736fd9d7c.json. Report an issue: GitHub.