Skip to main content

Function: CompositeKey()

CompositeKey(columnNames): ClassDecorator

Defined in: src/decorators/CompositeKey.ts:30

Class decorator designating a multi-column composite primary key for an entity.

When applied, DDL generation emits a table-level PRIMARY KEY (col1, col2) constraint instead of adding PRIMARY KEY inline on individual columns. Individual @PrimaryKey() decorators on the listed properties should still be present for ORM-level awareness.

Parameters​

ParameterTypeDescription
columnNamesstring[]The column names (not property names) that together form the PK.

Returns​

ClassDecorator

Usecase​

Junction tables, order-line items, and any entity without a single surrogate key.

Example​

@Entity()
@Table('order_items')
@CompositeKey(['order_id', 'product_id'])
export class OrderItem {
@PrimaryKey()
@Column({ name: 'order_id' })
orderId!: number;

@PrimaryKey()
@Column({ name: 'product_id' })
productId!: number;

quantity!: number;
}