{"id":"51c58673f49020bf","repo":"typeorm/typeorm","slug":"entity-entitymetadata-name-does-not-have-a-pr","errorCode":null,"errorMessage":"Entity \"${entityMetadata.name}\" does not have a primary column. Primary column is required to have in all your entities. Use @PrimaryColumn decorator to add a primary column to your entity.","messagePattern":"Entity \"(.+?)\" does not have a primary column\\. Primary column is required to have in all your entities\\. Use @PrimaryColumn decorator to add a primary column to your entity\\.","errorType":"exception","errorClass":"MissingPrimaryColumnError","httpStatus":null,"severity":"critical","filePath":"src/metadata-builder/EntityMetadataValidator.ts","lineNumber":66,"sourceCode":"        this.validateDependencies(entityMetadatas)\n        this.validateEagerRelations(entityMetadatas)\n    }\n\n    /**\n     * Validates given entity metadata.\n     *\n     * @param entityMetadata\n     * @param allEntityMetadatas\n     * @param driver\n     */\n    validate(\n        entityMetadata: EntityMetadata,\n        allEntityMetadatas: EntityMetadata[],\n        driver: Driver,\n    ) {\n        // check if table metadata has an id\n        if (!entityMetadata.primaryColumns.length && !entityMetadata.isJunction)\n            throw new MissingPrimaryColumnError(entityMetadata)\n\n        // if entity has multiple primary keys and uses custom constraint name,\n        // then all primary keys should have the same constraint name\n        if (entityMetadata.primaryColumns.length > 1) {\n            const areConstraintNamesEqual = entityMetadata.primaryColumns.every(\n                (columnMetadata, i, columnMetadatas) =>\n                    columnMetadata.primaryKeyConstraintName ===\n                    columnMetadatas[0].primaryKeyConstraintName,\n            )\n            if (!areConstraintNamesEqual) {\n                throw new TypeORMError(\n                    `Entity ${entityMetadata.name} has multiple primary columns with different constraint names. Constraint names should be the equal.`,\n                )\n            }\n        }\n\n        // validate if table is using inheritance it has a discriminator\n        // also validate if discriminator values are not empty and not repeated","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/typeorm/typeorm/blob/04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96/src/metadata-builder/EntityMetadataValidator.ts#L48-L84","documentation":"`MissingPrimaryColumnError` is thrown during metadata validation when a non-junction entity has zero `@PrimaryColumn`/`@PrimaryGeneratedColumn` columns. TypeORM requires a primary key for every entity to support updates, deletes, and relation joins.","triggerScenarios":"Defining an entity class with only `@Column()` fields and no primary decorator; mapping a table or view without designating a key.","commonSituations":"Forgetting `@PrimaryGeneratedColumn()` when creating an entity; mapping a database view that has no natural key.","solutions":["Add `@PrimaryGeneratedColumn()` (or `@PrimaryColumn`) to the entity","If it is a junction table, model it as a many-to-many relation rather than a standalone entity"],"exampleFix":"// before\n@Entity()\nclass User {\n  @Column() id: number\n}\n// after\n@Entity()\nclass User {\n  @PrimaryGeneratedColumn() id: number\n}","handlingStrategy":"validation","validationCode":"function assertHasPrimaryKey(entity: Function) {\n  const meta = dataSource.getMetadata(entity)\n  if (!meta.primaryColumns.length && !meta.isJunction) {\n    throw new Error(`${entity.name} has no primary column; add @PrimaryGeneratedColumn()`)\n  }\n}","typeGuard":"function hasPrimaryKey(metadata: EntityMetadata): boolean {\n  return metadata.primaryColumns.length > 0 || metadata.isJunction\n}","tryCatchPattern":null,"preventionTips":["Always define exactly one @PrimaryGeneratedColumn (or a composite @PrimaryColumn set) per entity","Run a metadata validation test over all entities in CI"],"tags":["primary-key","entity","validation"],"analyzedSha":"04ff4daedcf60fa4ffd0d5d33bbafaac1a9bbc96","analyzedAt":"2026-08-03T18:27:32.281Z","schemaVersion":2}