{"id":"c355e4d4ebb18046","repo":"drizzle-team/drizzle-orm","slug":"reason-you-can-t-specify-fktablename-as-p","errorCode":null,"errorMessage":"${reason}.\nYou can't specify \"${fkTableName}\" as parameter in ${table.name}.with object.\n\nFor more details, check this: https://orm.drizzle.team/docs/guides/seeding-using-with-option","messagePattern":"(.+?)\\.\nYou can't specify \"(.+?)\" as parameter in (.+?)\\.with object\\.\n\nFor more details, check this: https://orm\\.drizzle\\.team/docs/guides/seeding-using-with-option","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-seed/src/services/SeedService.ts","lineNumber":116,"sourceCode":"\t\t\t\t\ttablesPossibleGenerators[i]!.count = refinements[table.name]!.count;\n\t\t\t\t}\n\n\t\t\t\tif (refinements[table.name]!.with !== undefined) {\n\t\t\t\t\ttablesPossibleGenerators[i]!.count = refinements[table.name]!.count\n\t\t\t\t\t\t|| options?.count\n\t\t\t\t\t\t|| this.defaultCountForTable;\n\t\t\t\t\tlet idx: number;\n\t\t\t\t\tfor (\n\t\t\t\t\t\tconst fkTableName of Object.keys(\n\t\t\t\t\t\t\trefinements[table.name]!.with as {},\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\tif (!tablesInOutRelations[table.name]?.dependantTableNames.has(fkTableName)) {\n\t\t\t\t\t\t\tconst reason = tablesInOutRelations[table.name]?.selfRelation === true\n\t\t\t\t\t\t\t\t? `\"${table.name}\" table has self reference`\n\t\t\t\t\t\t\t\t: `\"${fkTableName}\" table doesn't have a reference to \"${table.name}\" table or`\n\t\t\t\t\t\t\t\t\t+ `\\nyou didn't include your one-to-many relation in the seed function schema`;\n\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t`${reason}.` + `\\nYou can't specify \"${fkTableName}\" as parameter in ${table.name}.with object.`\n\t\t\t\t\t\t\t\t\t+ `\\n\\nFor more details, check this: https://orm.drizzle.team/docs/guides/seeding-using-with-option`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tidx = tablesPossibleGenerators.findIndex(\n\t\t\t\t\t\t\t(table) => table.tableName === fkTableName,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tif (idx !== -1) {\n\t\t\t\t\t\t\tlet newTableWithCount: number,\n\t\t\t\t\t\t\t\tweightedCountSeed: number | undefined;\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\ttypeof refinements![table.name]!.with![fkTableName] === 'number'\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tnewTableWithCount = (tablesPossibleGenerators[i]!.withCount\n\t\t\t\t\t\t\t\t\t|| tablesPossibleGenerators[i]!.count)!\n\t\t\t\t\t\t\t\t\t* (refinements[table.name]!.with![fkTableName] as number);\n\t\t\t\t\t\t\t} else {","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-seed/src/services/SeedService.ts#L98-L134","documentation":"The refinements `.with` option lets you seed related rows: for table T you specify { with: { otherTable: ratioOrWeights } }. drizzle-seed validates that otherTable is a known dependant of T in the parsed relations. If otherTable has no reference back to T, or is a self-reference, or the one-to-many relation was not included in the schema passed to seed(), the `.with` entry is rejected.","triggerScenarios":"Specifying refinements[T].with = { X: 2 } when table X does not foreign-key into T; using `.with` on a table that has a self-referential FK; or forgetting to export a relation in the schema object so drizzle-seed never sees the T<->X link.","commonSituations":"Schema refactor that removed or renamed a relation but left stale refinements; passing a partial schema to seed() that omits the related table; misunderstanding `.with` direction (it must follow the FK from the dependant side).","solutions":["Verify the table named in `.with` actually has a foreign key referencing the current table and that the relation is exported in the schema passed to seed().","If it is a self-referential table, remove the `.with` entry for the self-reference and handle self-FK seeding via refinements on the column instead.","Correct the direction: `.with` keys must be tables that depend on (FK into) the current table, not the reverse."],"exampleFix":"// before — 'orders' has no FK back to 'users', or relation not in schema\nrefinements = { users: { with: { orders: 3 } } }\n// after — ensure orders.userId FK exists and schema includes it\nexport const schema = { users, orders, ... };\nawait seed(db, { schema }, { count: 10 });","handlingStrategy":"validation","validationCode":"// Before seeding, verify every table in a `.with` refinement has a FK back to the parent\n// and that both tables are in the schema object.\nfunction validateWithOption(\n  schema: Record<string, any>,\n  refinements: Record<string, any>,\n  getRelations: (schema: Record<string, any>) => Map<string, Set<string>>\n): string[] {\n  const errors: string[] = [];\n  const dependants = getRelations(schema); // tableName -> Set of tables that FK into it\n  for (const [table, ref] of Object.entries(refinements)) {\n    if (!ref.with) continue;\n    for (const fkTable of Object.keys(ref.with)) {\n      if (!dependants.get(table)?.has(fkTable)) {\n        errors.push(`${table}.with references '${fkTable}' which is not a dependant. Check FK direction and schema inclusion.`);\n      }\n    }\n  }\n  return errors;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass the full set of related tables in the schema object to seed().","Double-check FK direction: `.with` keys are tables that reference the parent, not tables the parent references.","Keep refinements in sync with schema changes — remove stale `.with` entries after relation refactors."],"tags":["refinements","with-option","relations","foreign-key"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}