drizzle-team/drizzle-orm · error · Error

Column '${col.name}' has not null contraint, and you didn't

Error message

Column '${col.name}' has not null contraint,
and you didn't specify a table for foreign key on column '${col.name}' in '${table.name}' table.

For more details, check this: https://orm.drizzle.team/docs/guides/seeding-with-partially-exposed-tables#example-1

What it means

When a column is a foreign key with a NOT NULL constraint, drizzle-seed needs a source table to pull valid FK values from. If the referenced table is not included in the schema passed to seed() (a 'partially exposed' schema) and no generator is refined for the column, the seeder cannot produce a non-null value and throws.

Source

Thrown at drizzle-seed/src/services/SeedService.ts:220

					columnPossibleGenerator.generator = genObj;
					columnPossibleGenerator.wasRefined = true;
				} else if (Object.hasOwn(foreignKeyColumns, col.name)) {
					// TODO: I might need to assign repeatedValuesCount to column there instead of doing so in generateTablesValues
					const cyclicRelation = relations.find((rel) =>
						rel.table === table.name
						&& rel.isCyclic === true
						&& rel.columns.includes(col.name)
					);

					if (cyclicRelation !== undefined) {
						columnPossibleGenerator.isCyclic = true;
					}

					if (
						(foreignKeyColumns[col.name]?.table === undefined || !tableNamesSet.has(foreignKeyColumns[col.name]!.table))
						&& col.notNull === true
					) {
						throw new Error(
							`Column '${col.name}' has not null contraint,`
								+ `\nand you didn't specify a table for foreign key on column '${col.name}' in '${table.name}' table.`
								+ `\n\nFor more details, check this: https://orm.drizzle.team/docs/guides/seeding-with-partially-exposed-tables#example-1`,
						);
					}

					const predicate = (
						cyclicRelation !== undefined
						|| (
							foreignKeyColumns[col.name]?.table === undefined
							|| !tableNamesSet.has(foreignKeyColumns[col.name]!.table)
						)
					)
						&& col.notNull === false;

					if (predicate === true) {
						if (
							(foreignKeyColumns[col.name]?.table === undefined

View on GitHub (pinned to b7862528fd)

Solutions

  1. Include the referenced table in the schema object passed to seed() so the seeder can draw real FK values.
  2. If the referenced table genuinely cannot be included, relax the column's NOT NULL constraint to nullable so drizzle-seed fills it with null.
  3. Provide an explicit generator in refinements for the FK column that supplies valid values independently.

Example fix

// before — schema omits the 'departments' table referenced by users.deptId (notNull)
await seed(db, { schema: { users } }, { count: 10 });
// after — include the referenced table
await seed(db, { schema: { users, departments } }, { count: 10 });
Defensive patterns

Strategy: validation

Validate before calling

// Before seeding, verify every NOT NULL FK column has its referenced table in the schema.
function findMissingNotNullFkTables(
  tables: Array<{ name: string; columns: Array<{ name: string; columnType: string; notNull: boolean; references?: string }> }>,
  schemaTableNames: Set<string>
): string[] {
  const missing: string[] = [];
  for (const t of tables) {
    for (const c of t.columns) {
      if (c.notNull && c.references && !schemaTableNames.has(c.references)) {
        missing.push(`${t.name}.${c.name} -> ${c.references}`);
      }
    }
  }
  return missing;
}

Prevention

When it happens

Trigger: Passing a subset of tables to seed() that excludes a table referenced by a NOT NULL FK column; or the FK's referenced table name does not match any table in the provided schema set.

Common situations: Seeding a focused slice of a large schema for testing; splitting schema exports and forgetting to include a parent table; renamed/migrated tables where the FK target name no longer matches.

Related errors


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