You can create a relationship on a column other than the primary key using TypeORM. Here’s an example
@Entity()
export class ExternalCommentEntity {
@Index({ unique: true }) // ⚠️ Referenced column must have a unique constraint
@Column({ nullable: true })
externalId?: string;
@Column({ nullable: true })
@RelationId((event: ExternalCommentEntity) => event.parent)
parentExternalId?: string | undefined;
@ManyToOne(() => ExternalCommentEntity)
@JoinColumn({
referencedColumnName: "externalId", // ℹ️ Specify the name of your other unique column
})
parent?: ExternalCommentEntity;
}