@ForeignKey
@ForeignKey records a foreign-key relationship from a column to another entity. See
Indexes and Foreign Keys for the full guide.
function ForeignKey(type: TypeThunk, targetKey?: string): PropertyDecorator;
@Entity({ tableName: 'customer_tags' })
@PrimaryKey(['customerId', 'tagId'])
class CustomerTag {
@Column({ fieldName: 'customer_id', notNull: true })
@ForeignKey(async () => (await import('./customer.entity.js')).Customer)
declare customerId: number;
@Column({ fieldName: 'tag_id', notNull: true })
declare tagId: number;
}
type— the referenced entity, or a thunk (() => Type | Promise<Type>) to avoid circular imports, same as@Linkand@Embedded.targetKey— the referenced column on the target entity. When omitted, it's resolved the same way an unqualified@Linkresolves its target key (defaults to the target's single-column primary key).
Declared foreign keys are stored on entity.foreignKeys and are looked up automatically by
@Link when a link declares no explicit sourceKey/targetKey of
its own. Like @Index, @ForeignKey only records metadata for @sqb/connect's own
use (key resolution, introspection) — it does not emit a DDL constraint.