{"record":{"id":"5066047f60a6c393","repo":"n8n-io/n8n","slug":"entity-entitymetadata-name-has-multiple-primary","errorCode":null,"errorMessage":"Entity ${entityMetadata.name} has multiple primary columns with different constraint names. Constraint names should be the equal.","messagePattern":"Entity (.+?) has multiple primary columns with different constraint names\\. Constraint names should be the equal\\.","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/typeorm/src/metadata-builder/EntityMetadataValidator.ts","lineNumber":63,"sourceCode":"\t}\n\n\t/**\n\t * Validates given entity metadata.\n\t */\n\tvalidate(entityMetadata: EntityMetadata, allEntityMetadatas: EntityMetadata[], driver: Driver) {\n\t\t// check if table metadata has an id\n\t\tif (!entityMetadata.primaryColumns.length && !entityMetadata.isJunction)\n\t\t\tthrow new MissingPrimaryColumnError(entityMetadata);\n\n\t\t// if entity has multiple primary keys and uses custom constraint name,\n\t\t// then all primary keys should have the same constraint name\n\t\tif (entityMetadata.primaryColumns.length > 1) {\n\t\t\tconst areConstraintNamesEqual = entityMetadata.primaryColumns.every(\n\t\t\t\t(columnMetadata, i, columnMetadatas) =>\n\t\t\t\t\tcolumnMetadata.primaryKeyConstraintName === columnMetadatas[0].primaryKeyConstraintName,\n\t\t\t);\n\t\t\tif (!areConstraintNamesEqual) {\n\t\t\t\tthrow new TypeORMError(\n\t\t\t\t\t`Entity ${entityMetadata.name} has multiple primary columns with different constraint names. Constraint names should be the equal.`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\t// validate if table is using inheritance it has a discriminator\n\t\t// also validate if discriminator values are not empty and not repeated\n\t\tif (\n\t\t\tentityMetadata.inheritancePattern === 'STI' ||\n\t\t\tentityMetadata.tableType === 'entity-child'\n\t\t) {\n\t\t\tif (!entityMetadata.discriminatorColumn)\n\t\t\t\tthrow new TypeORMError(\n\t\t\t\t\t`Entity ${entityMetadata.name} using single-table inheritance, it should also have a discriminator column. Did you forget to put discriminator column options?`,\n\t\t\t\t);\n\n\t\t\tif (typeof entityMetadata.discriminatorValue === 'undefined')\n\t\t\t\tthrow new TypeORMError(","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/metadata-builder/EntityMetadataValidator.ts#L45-L81","documentation":"Thrown by EntityMetadataValidator.validate when an entity has more than one primary column but they do not all share the same primaryKeyConstraintName. The check uses entityMetadata.primaryColumns.every comparing each column's primaryKeyConstraintName to the first; mismatch triggers TypeORMError. Without a shared constraint name the database cannot create a single composite PK constraint.","triggerScenarios":"Declaring multiple @PrimaryColumn decorators with different primaryKeyConstraintName values, or mixing some columns that set the option with others that leave it undefined.","commonSituations":"Adopting named PK constraints (e.g. for cross-database consistency) on only some columns of a composite key; copy-pasting columns from different entities.","solutions":["Set the identical primaryKeyConstraintName on every @PrimaryColumn of the composite key.","If you do not need a custom name, omit the option on all of them (they will all be undefined and pass the every() check).","Audit the entity and align the option values to a single constant."],"exampleFix":"// before\n@Entity()\nexport class Member {\n  @PrimaryColumn({ primaryKeyConstraintName: 'pk_org' }) orgId: number;\n  @PrimaryColumn({ primaryKeyConstraintName: 'pk_user' }) userId: number;\n}\n\n// after\n@Entity()\nexport class Member {\n  @PrimaryColumn({ primaryKeyConstraintName: 'pk_member' }) orgId: number;\n  @PrimaryColumn({ primaryKeyConstraintName: 'pk_member' }) userId: number;\n}","handlingStrategy":"validation","validationCode":"function assertCompositePkConstraintNamesAligned(dataSource: DataSource): string[] {\n  const problems: string[] = [];\n  for (const meta of dataSource.entityMetadatas) {\n    if (meta.primaryColumns.length <= 1) continue;\n    const names = meta.primaryColumns.map((c) => c.primaryKeyConstraintName);\n    if (!names.every((n) => n === names[0])) {\n      problems.push(`${meta.targetName} primary columns have mismatched constraint names: ${JSON.stringify(names)}`);\n    }\n  }\n  return problems;\n}","typeGuard":null,"tryCatchPattern":"try {\n  await dataSource.initialize();\n} catch (err) {\n  if (err.message.includes('multiple primary columns with different constraint names')) {\n    // align primaryKeyConstraintName across composite PK columns\n  }\n  throw err;\n}","preventionTips":["Define composite PKs with a single shared const for primaryKeyConstraintName.","Run the post-init constraint-name validator in CI.","When adding a new column to a composite PK, copy the constraint-name option from a sibling column."],"tags":["primary-key","constraint-name","composite-key","startup"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}