{"id":"cb9299b4449504ef","repo":"drizzle-team/drizzle-orm","slug":"the-table1-name-and-table2-name-tables-h","errorCode":null,"errorMessage":"The '${table1.name}' and '${table2.name}' tables have not null foreign keys. You can't seed cyclic tables with not null foreign key columns.","messagePattern":"The '(.+?)' and '(.+?)' tables have not null foreign keys\\. You can't seed cyclic tables with not null foreign key columns\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-seed/src/services/SeedService.ts","lineNumber":356,"sourceCode":"\n\tcyclicTablesCompare = (\n\t\ttable1: Table,\n\t\ttable2: Table,\n\t\trelation: Relation & { isCyclic: boolean },\n\t\treverseRelation: Relation & { isCyclic: boolean } | undefined,\n\t) => {\n\t\t// TODO: revise\n\t\tconst hasTable1NotNullColumns = relation.columns.some((colIName) =>\n\t\t\ttable1.columns.find((colJ) => colJ.name === colIName)?.notNull === true\n\t\t);\n\n\t\tif (reverseRelation !== undefined) {\n\t\t\tconst hasTable2NotNullColumns = reverseRelation.columns.some((colIName) =>\n\t\t\t\ttable2.columns.find((colJ) => colJ.name === colIName)?.notNull === true\n\t\t\t);\n\n\t\t\tif (hasTable1NotNullColumns && hasTable2NotNullColumns) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`The '${table1.name}' and '${table2.name}' tables have not null foreign keys. You can't seed cyclic tables with not null foreign key columns.`,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (hasTable1NotNullColumns) return 1;\n\t\t\telse if (hasTable2NotNullColumns) return -1;\n\t\t\treturn 0;\n\t\t}\n\n\t\tif (hasTable1NotNullColumns) {\n\t\t\treturn 1;\n\t\t}\n\t\treturn 0;\n\n\t\t// if (hasTable1NotNullColumns) return 1;\n\t\t// else if (hasTable2NotNullColumns) return -1;\n\t};\n","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-seed/src/services/SeedService.ts#L338-L374","documentation":"When two tables reference each other cyclically and BOTH sides of the cycle have NOT NULL foreign-key columns, drizzle-seed cannot insert either table first: each row requires a valid non-null FK value from the other table that does not yet exist. The cyclicTablesCompare comparator detects this deadlock and throws.","triggerScenarios":"Two tables A and B where A has a NOT NULL FK to B and B has a NOT NULL FK to A (mutual mandatory dependency); the seeder needs to order them but neither can go first.","commonSituations":"Modeling bidirectional relationships with mandatory links (e.g. User.profileId NOT NULL and Profile.userId NOT NULL); schema where a deferred/nullable FK was accidentally made NOT NULL.","solutions":["Make at least one side of the cycle nullable so drizzle-seed can insert that table first, back-fill, then update.","Use database-level deferred FK constraints (Postgres DEFERRABLE INITIALLY DEFERRED) and set the column nullable for seeding.","Break the cycle by seeding one table with a refined generator that supplies a placeholder/valid value, then seed the other."],"exampleFix":"// before — mutual NOT NULL FKs\nusers: { profileId: integer().notNull().references(() => profiles.id) }\nprofiles: { userId: integer().notNull().references(() => users.id) }\n// after — make one side nullable\nusers: { profileId: integer().references(() => profiles.id) }","handlingStrategy":"validation","validationCode":"// Detect mutual NOT NULL FK cycles before seeding.\nfunction hasMutualNotNullCycle(\n  relations: Array<{ table: string; refTable: string; columns: string[]; isCyclic: boolean }>,\n  columnNotNull: (table: string, col: string) => boolean\n): boolean {\n  for (const r of relations) {\n    if (!r.isCyclic) continue;\n    const reverse = relations.find((x) => x.table === r.refTable && x.refTable === r.table);\n    if (!reverse) continue;\n    const sideA = r.columns.some((c) => columnNotNull(r.table, c));\n    const sideB = reverse.columns.some((c) => columnNotNull(reverse.table, c));\n    if (sideA && sideB) return true;\n  }\n  return false;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always make at least one side of a cyclic FK nullable.","Use Postgres DEFERRABLE INITIALLY DEFERRED for cyclic constraints in production.","Review cyclic relationships during schema design for seeding feasibility."],"tags":["cyclic","foreign-key","not-null","relations"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}