{"record":{"id":"c95682d3d40b4c05","repo":"n8n-io/n8n","slug":"entity-whose-relation-needs-to-be-set-is-not-set","errorCode":null,"errorMessage":"Entity whose relation needs to be set is not set. Use .of method to define whose relation you want to set.","messagePattern":"Entity whose relation needs to be set is not set\\. Use \\.of method to define whose relation you want to set\\.","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/typeorm/src/query-builder/RelationQueryBuilder.ts","lineNumber":50,"sourceCode":"\t * Sets entity (target) which relations will be updated.\n\t */\n\tof(entity: any | any[]): this {\n\t\tthis.expressionMap.of = entity;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Sets entity relation's value.\n\t * Value can be entity, entity id or entity id map (if entity has composite ids).\n\t * Works only for many-to-one and one-to-one relations.\n\t * For many-to-many and one-to-many relations use #add and #remove methods instead.\n\t */\n\tasync set(value: any): Promise<void> {\n\t\tconst relation = this.expressionMap.relationMetadata;\n\n\t\tif (!this.expressionMap.of)\n\t\t\t// todo: move this check before relation query builder creation?\n\t\t\tthrow new TypeORMError(\n\t\t\t\t`Entity whose relation needs to be set is not set. Use .of method to define whose relation you want to set.`,\n\t\t\t);\n\n\t\tif (relation.isManyToMany || relation.isOneToMany)\n\t\t\tthrow new TypeORMError(\n\t\t\t\t`Set operation is only supported for many-to-one and one-to-one relations. ` +\n\t\t\t\t\t`However given \"${relation.propertyPath}\" has ${relation.relationType} relation. ` +\n\t\t\t\t\t`Use .add() method instead.`,\n\t\t\t);\n\n\t\t// if there are multiple join columns then user must send id map as \"value\" argument. check if he really did it\n\t\tif (\n\t\t\trelation.joinColumns &&\n\t\t\trelation.joinColumns.length > 1 &&\n\t\t\t(!ObjectUtils.isObject(value) || Object.keys(value).length < relation.joinColumns.length)\n\t\t)\n\t\t\tthrow new TypeORMError(\n\t\t\t\t`Value to be set into the relation must be a map of relation ids, for example: .set({ firstName: \"...\", lastName: \"...\" })`,","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/query-builder/RelationQueryBuilder.ts#L32-L68","documentation":"RelationQueryBuilder.set throws TypeORMError before touching the DB if this.expressionMap.of is unset. .of(entityOrId) is what binds the operation to a concrete parent row; without it TypeORM cannot know which row to update, so it refuses rather than issue an unbounded update.","triggerScenarios":"Calling qb.relation('User.profile').set(profile) and forgetting .of(user); calling .set() before .of() in the chain (note .of returns this so order matters only if you never call it).","commonSituations":"Helper that conditionally sets a relation and skips the .of() in an edge branch; refactor that moves .of() into a separate function which is not always invoked.","solutions":["Always chain .of() before .set(): qb.relation(Entity,'rel').of(parent).set(value).","If the parent is dynamic, guard in application code: if (!parent?.id) throw new UserError('parent required') before building the relation call.","Add a unit test asserting .of is set when the helper is invoked."],"exampleFix":"// before\nawait qb.relation(User,'profile').set(profileId);\n// after\nawait qb.relation(User,'profile').of(user).set(profileId);","handlingStrategy":"validation","validationCode":"function requireOf<T>(qb: any, parent: T | undefined | null): T {\n  if (parent == null) throw new Error('parent entity is required to .set() a relation');\n  qb.of(parent);\n  return parent;\n}","typeGuard":"function hasOf(qb: any): boolean { return qb.expressionMap.of != null; }","tryCatchPattern":null,"preventionTips":["Make .of() the first call after .relation() so it is never forgotten.","Reject null parents at the service boundary before constructing the relation call.","Add a unit test that exercises the relation helper with a null parent and asserts a clean error."],"tags":["typeorm","relation-query-builder","set","of-method"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}