{"id":"2a8cb469f6ff9678","repo":"drizzle-team/drizzle-orm","slug":"column-col-name-has-not-null-contraint-and-y","errorCode":null,"errorMessage":"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","messagePattern":"Column '(.+?)' has not null contraint,\nand you didn't specify a table for foreign key on column '(.+?)' in '(.+?)' table\\.\n\nFor more details, check this: https://orm\\.drizzle\\.team/docs/guides/seeding-with-partially-exposed-tables#example-1","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-seed/src/services/SeedService.ts","lineNumber":220,"sourceCode":"\t\t\t\t\tcolumnPossibleGenerator.generator = genObj;\n\t\t\t\t\tcolumnPossibleGenerator.wasRefined = true;\n\t\t\t\t} else if (Object.hasOwn(foreignKeyColumns, col.name)) {\n\t\t\t\t\t// TODO: I might need to assign repeatedValuesCount to column there instead of doing so in generateTablesValues\n\t\t\t\t\tconst cyclicRelation = relations.find((rel) =>\n\t\t\t\t\t\trel.table === table.name\n\t\t\t\t\t\t&& rel.isCyclic === true\n\t\t\t\t\t\t&& rel.columns.includes(col.name)\n\t\t\t\t\t);\n\n\t\t\t\t\tif (cyclicRelation !== undefined) {\n\t\t\t\t\t\tcolumnPossibleGenerator.isCyclic = true;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (\n\t\t\t\t\t\t(foreignKeyColumns[col.name]?.table === undefined || !tableNamesSet.has(foreignKeyColumns[col.name]!.table))\n\t\t\t\t\t\t&& col.notNull === true\n\t\t\t\t\t) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`Column '${col.name}' has not null contraint,`\n\t\t\t\t\t\t\t\t+ `\\nand you didn't specify a table for foreign key on column '${col.name}' in '${table.name}' table.`\n\t\t\t\t\t\t\t\t+ `\\n\\nFor more details, check this: https://orm.drizzle.team/docs/guides/seeding-with-partially-exposed-tables#example-1`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst predicate = (\n\t\t\t\t\t\tcyclicRelation !== undefined\n\t\t\t\t\t\t|| (\n\t\t\t\t\t\t\tforeignKeyColumns[col.name]?.table === undefined\n\t\t\t\t\t\t\t|| !tableNamesSet.has(foreignKeyColumns[col.name]!.table)\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t\t\t&& col.notNull === false;\n\n\t\t\t\t\tif (predicate === true) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t(foreignKeyColumns[col.name]?.table === undefined","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-seed/src/services/SeedService.ts#L202-L238","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Include the referenced table in the schema object passed to seed() so the seeder can draw real FK values.","If the referenced table genuinely cannot be included, relax the column's NOT NULL constraint to nullable so drizzle-seed fills it with null.","Provide an explicit generator in refinements for the FK column that supplies valid values independently."],"exampleFix":"// before — schema omits the 'departments' table referenced by users.deptId (notNull)\nawait seed(db, { schema: { users } }, { count: 10 });\n// after — include the referenced table\nawait seed(db, { schema: { users, departments } }, { count: 10 });","handlingStrategy":"validation","validationCode":"// Before seeding, verify every NOT NULL FK column has its referenced table in the schema.\nfunction findMissingNotNullFkTables(\n  tables: Array<{ name: string; columns: Array<{ name: string; columnType: string; notNull: boolean; references?: string }> }>,\n  schemaTableNames: Set<string>\n): string[] {\n  const missing: string[] = [];\n  for (const t of tables) {\n    for (const c of t.columns) {\n      if (c.notNull && c.references && !schemaTableNames.has(c.references)) {\n        missing.push(`${t.name}.${c.name} -> ${c.references}`);\n      }\n    }\n  }\n  return missing;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass the complete schema (all related tables) to seed().","Make FK columns nullable if the referenced table won't be included in seeding.","Audit for NOT NULL FKs after schema migrations."],"tags":["foreign-key","not-null","partial-schema","relations"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}