{"id":"b1780a2c53a320df","repo":"drizzle-team/drizzle-orm","slug":"invalid-relation-relation-fieldname-for-table","errorCode":null,"errorMessage":"Invalid relation \"${relation.fieldName}\" for table \"${it.schema ? `${it.schema}.${it.dbName}` : it.dbName}\"","messagePattern":"Invalid relation \"(.+?)\" for table \"(.+?)\\.(.+?)` : it\\.dbName\\}\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"drizzle-kit/src/serializer/studio.ts","lineNumber":574,"sourceCode":"\t\t\t\t\t\ttype = 'one';\n\t\t\t\t\t} else if (is(rel, Many)) {\n\t\t\t\t\t\ttype = 'many';\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new Error('unsupported relation type');\n\t\t\t\t\t}\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\tname,\n\t\t\t\t\t\ttype,\n\t\t\t\t\t\ttable: it.dbName,\n\t\t\t\t\t\tschema: it.schema || 'public',\n\t\t\t\t\t\tcolumns: fields,\n\t\t\t\t\t\trefTable: refTableName,\n\t\t\t\t\t\trefSchema: refSchema || 'public',\n\t\t\t\t\t\trefColumns: refColumns,\n\t\t\t\t\t};\n\t\t\t\t} catch {\n\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t`Invalid relation \"${relation.fieldName}\" for table \"${\n\t\t\t\t\t\t\tit.schema ? `${it.schema}.${it.dbName}` : it.dbName\n\t\t\t\t\t\t}\"`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t})\n\t\t)\n\t\t.flat();\n\treturn relations;\n};\n\nconst init = z.object({\n\ttype: z.literal('init'),\n});\n\nconst proxySchema = z.object({\n\ttype: z.literal('proxy'),\n\tdata: z.object({","sourceCodeStart":556,"sourceCodeEnd":592,"githubUrl":"https://github.com/drizzle-team/drizzle-orm/blob/b7862528fd8fc39bc2653a6c18dad7c1f4e68d10/drizzle-kit/src/serializer/studio.ts#L556-L592","documentation":"A catch-all wrapper around `normalizeRelation` and the dialect/relation-type checks. Any exception during relation normalization (ambiguous fields, mismatched column counts, unsupported dialect, non-One/Many) is re-thrown with a message naming the offending `relation.fieldName` and its parent table. This is the user-facing error for a structurally broken relation.","triggerScenarios":"Defining a `one()` relation whose `fields` and `references` arrays have different lengths, point at the wrong table, or are ambiguous (multiple relations to the same table without `relationName`). Also reached via errors 3/4 when the inner check fails.","commonSituations":"Ambiguous relations between two tables that share two foreign keys (the docs' disambiguation case), schema refactors that leave dangling references, or copy-paste errors in `fields`/`references`.","solutions":["Open the relation named in the message and verify `fields` and `references` have equal length and valid columns.","Add `relationName` to disambiguate multiple relations between the same two tables.","Re-run Studio; if it persists, temporarily comment the named relation to confirm the cause."],"exampleFix":"// before\none(posts, { fields: [users.id], references: [posts.authorId, posts.coAuthorId] }), // length mismatch\n// after\none(posts, { fields: [users.id], references: [posts.authorId] }),","handlingStrategy":"validation","validationCode":"// Validate one() fields/references length parity before Studio\nimport type { AnyColumn } from 'drizzle-orm';\nfunction assertOneRelation(fields: AnyColumn[], references: AnyColumn[]) {\n  if (fields.length !== references.length) {\n    throw new Error('one() fields and references length mismatch');\n  }\n}","typeGuard":"function isAmbiguousRelationPair(a: { table: string }, b: { table: string }) {\n  return a.table === b.table;\n}","tryCatchPattern":"try {\n  const rels = extractRelations(config);\n} catch (e) {\n  const m = (e as Error).message.match(/Invalid relation \"(\\w+)\" for table \"([\\w.]+)\"/);\n  if (m) console.error(`Fix relation ${m[1]} on ${m[2]}`);\n  throw e;\n}","preventionTips":["Always pair fields[] and references[] 1:1 in one().","Add relationName when two relations target the same table."],"tags":["drizzle-kit","studio","relations","schema"],"analyzedSha":"b7862528fd8fc39bc2653a6c18dad7c1f4e68d10","analyzedAt":"2026-08-03T18:11:14.318Z","schemaVersion":2}