{"record":{"id":"40dc2b43233fd511","repo":"n8n-io/n8n","slug":"relation-count-can-not-be-implemented-on-manytoone","errorCode":null,"errorMessage":"Relation count can not be implemented on ManyToOne or OneToOne relations.","messagePattern":"Relation count can not be implemented on ManyToOne or OneToOne relations\\.","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/typeorm/src/metadata-builder/EntityMetadataValidator.ts","lineNumber":104,"sourceCode":"\t\t\t\treturn (\n\t\t\t\t\tmetadata !== entityMetadata &&\n\t\t\t\t\t(metadata.inheritancePattern === 'STI' || metadata.tableType === 'entity-child') &&\n\t\t\t\t\tmetadata.tableName === entityMetadata.tableName &&\n\t\t\t\t\tmetadata.discriminatorValue === entityMetadata.discriminatorValue &&\n\t\t\t\t\tmetadata.inheritanceTree.some(\n\t\t\t\t\t\t(parent) => entityMetadata.inheritanceTree.indexOf(parent) !== -1,\n\t\t\t\t\t)\n\t\t\t\t);\n\t\t\t});\n\t\t\tif (sameDiscriminatorValueEntityMetadata)\n\t\t\t\tthrow new TypeORMError(\n\t\t\t\t\t`Entities ${entityMetadata.name} and ${sameDiscriminatorValueEntityMetadata.name} have the same discriminator values. Make sure they are different while using the @ChildEntity decorator.`,\n\t\t\t\t);\n\t\t}\n\n\t\tentityMetadata.relationCounts.forEach((relationCount) => {\n\t\t\tif (relationCount.relation.isManyToOne || relationCount.relation.isOneToOne)\n\t\t\t\tthrow new TypeORMError(\n\t\t\t\t\t`Relation count can not be implemented on ManyToOne or OneToOne relations.`,\n\t\t\t\t);\n\t\t});\n\n\t\tentityMetadata.columns\n\t\t\t.filter((column) => !column.isVirtualProperty)\n\t\t\t.forEach((column) => {\n\t\t\t\tconst normalizedColumn = driver.normalizeType(column) as ColumnType;\n\t\t\t\tif (driver.supportedDataTypes.indexOf(normalizedColumn) === -1)\n\t\t\t\t\tthrow new DataTypeNotSupportedError(column, normalizedColumn, driver.options.type);\n\t\t\t\tif (column.length && driver.withLengthColumnTypes.indexOf(normalizedColumn) === -1)\n\t\t\t\t\tthrow new TypeORMError(\n\t\t\t\t\t\t`Column ${column.propertyName} of Entity ${entityMetadata.name} does not support length property.`,\n\t\t\t\t\t);\n\t\t\t\tif (column.type === 'enum' && !column.enum && !column.enumName)\n\t\t\t\t\tthrow new TypeORMError(\n\t\t\t\t\t\t`Column \"${column.propertyName}\" of Entity \"${entityMetadata.name}\" is defined as enum, but missing \"enum\" or \"enumName\" properties.`,\n\t\t\t\t\t);","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/metadata-builder/EntityMetadataValidator.ts#L86-L122","documentation":"Thrown by EntityMetadataValidator.validate during the entityMetadata.relationCounts.forEach loop when a @RelationCount decorator targets a relation whose metadata reports isManyToOne or isOneToOne. Relation counts only make sense over collections (OneToMany / ManyToMany); counting the single related entity on the to-one side is meaningless.","triggerScenarios":"Decorating a ManyToOne or OneToOne property (or pointing @RelationCount at one) with @RelationCount.","commonSituations":"Misreading the relation cardinality; refactoring a OneToMany into ManyToOne but leaving the @RelationCount decorator behind.","solutions":["Move the @RelationCount to a OneToMany or ManyToMany relation on the entity.","Remove the @RelationCount decorator if the underlying relation is correctly to-one.","If you actually need the related entity's identifier, use @RelationId instead."],"exampleFix":"// before\n@ManyToOne(() => User, (u) => u.posts)\n@RelationCount(() => Post, 'author')\nauthor: User;\n\n// after\n@ManyToOne(() => User, (u) => u.posts)\nauthor: User;\n// (remove @RelationCount from the to-one side; use it only on collections)","handlingStrategy":"validation","validationCode":"function assertRelationCountOnlyOnCollections(dataSource: DataSource): string[] {\n  const problems: string[] = [];\n  for (const meta of dataSource.entityMetadatas) {\n    for (const rc of meta.relationCounts) {\n      if (rc.relation.isManyToOne || rc.relation.isOneToOne) {\n        problems.push(`${meta.targetName}.@RelationCount on to-one relation '${rc.relation.propertyPath}'`);\n      }\n    }\n  }\n  return problems;\n}","typeGuard":null,"tryCatchPattern":"try {\n  await dataSource.initialize();\n} catch (err) {\n  if (err.message.includes('Relation count can not be implemented on ManyToOne or OneToOne')) {\n    // move or remove the @RelationCount decorator\n  }\n  throw err;\n}","preventionTips":["Restrict @RelationCount usage to OneToMany / ManyToMany relations in code review.","Run the post-init relation-count validator in CI.","When converting a relation's cardinality, audit any @RelationCount referencing it."],"tags":["relation-count","many-to-one","one-to-one","metadata","startup"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}