n8n-io/n8n · error · TypeORMError

Relation count can not be implemented on ManyToOne or OneToO

Error message

Relation count can not be implemented on ManyToOne or OneToOne relations.

What it means

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.

Source

Thrown at packages/@n8n/typeorm/src/metadata-builder/EntityMetadataValidator.ts:104

				return (
					metadata !== entityMetadata &&
					(metadata.inheritancePattern === 'STI' || metadata.tableType === 'entity-child') &&
					metadata.tableName === entityMetadata.tableName &&
					metadata.discriminatorValue === entityMetadata.discriminatorValue &&
					metadata.inheritanceTree.some(
						(parent) => entityMetadata.inheritanceTree.indexOf(parent) !== -1,
					)
				);
			});
			if (sameDiscriminatorValueEntityMetadata)
				throw new TypeORMError(
					`Entities ${entityMetadata.name} and ${sameDiscriminatorValueEntityMetadata.name} have the same discriminator values. Make sure they are different while using the @ChildEntity decorator.`,
				);
		}

		entityMetadata.relationCounts.forEach((relationCount) => {
			if (relationCount.relation.isManyToOne || relationCount.relation.isOneToOne)
				throw new TypeORMError(
					`Relation count can not be implemented on ManyToOne or OneToOne relations.`,
				);
		});

		entityMetadata.columns
			.filter((column) => !column.isVirtualProperty)
			.forEach((column) => {
				const normalizedColumn = driver.normalizeType(column) as ColumnType;
				if (driver.supportedDataTypes.indexOf(normalizedColumn) === -1)
					throw new DataTypeNotSupportedError(column, normalizedColumn, driver.options.type);
				if (column.length && driver.withLengthColumnTypes.indexOf(normalizedColumn) === -1)
					throw new TypeORMError(
						`Column ${column.propertyName} of Entity ${entityMetadata.name} does not support length property.`,
					);
				if (column.type === 'enum' && !column.enum && !column.enumName)
					throw new TypeORMError(
						`Column "${column.propertyName}" of Entity "${entityMetadata.name}" is defined as enum, but missing "enum" or "enumName" properties.`,
					);

View on GitHub (pinned to 5ac6606e81)

Solutions

  1. Move the @RelationCount to a OneToMany or ManyToMany relation on the entity.
  2. Remove the @RelationCount decorator if the underlying relation is correctly to-one.
  3. If you actually need the related entity's identifier, use @RelationId instead.

Example fix

// before
@ManyToOne(() => User, (u) => u.posts)
@RelationCount(() => Post, 'author')
author: User;

// after
@ManyToOne(() => User, (u) => u.posts)
author: User;
// (remove @RelationCount from the to-one side; use it only on collections)
Defensive patterns

Strategy: validation

Validate before calling

function assertRelationCountOnlyOnCollections(dataSource: DataSource): string[] {
  const problems: string[] = [];
  for (const meta of dataSource.entityMetadatas) {
    for (const rc of meta.relationCounts) {
      if (rc.relation.isManyToOne || rc.relation.isOneToOne) {
        problems.push(`${meta.targetName}.@RelationCount on to-one relation '${rc.relation.propertyPath}'`);
      }
    }
  }
  return problems;
}

Try / catch

try {
  await dataSource.initialize();
} catch (err) {
  if (err.message.includes('Relation count can not be implemented on ManyToOne or OneToOne')) {
    // move or remove the @RelationCount decorator
  }
  throw err;
}

Prevention

When it happens

Trigger: Decorating a ManyToOne or OneToOne property (or pointing @RelationCount at one) with @RelationCount.

Common situations: Misreading the relation cardinality; refactoring a OneToMany into ManyToOne but leaving the @RelationCount decorator behind.

Related errors


AI-assisted analysis of n8n-io/n8n@5ac6606e81 (2026-08-12). Data as JSON: /api/errors/40dc2b43233fd511. Report an issue: GitHub.