Skip to main content
zerotal

Documentation


Documentation / @zerotal/orm / index / ForeignKeyBuilder

Class: ForeignKeyBuilder

Defined in: packages/orm/src/schema/ColumnDefinition.ts:416

Builds a table-level FOREIGN KEY constraint. Obtained from Blueprint.foreign, or indirectly via ForeignIdColumnBuilder.constrained. Configure the referenced table/column and referential actions by chaining, then the constraint is compiled by ForeignKeyBuilder.toConstraintSQL when the table is built.

Example

table.foreign('user_id').references('id').on('users').onDelete('CASCADE');
table.foreign('user_id').references('users.id').nullOnDelete();

Constructors

Constructor

new ForeignKeyBuilder(_column): ForeignKeyBuilder

Defined in: packages/orm/src/schema/ColumnDefinition.ts:422

Parameters

_column

string

Returns

ForeignKeyBuilder

Foreign keys

references()

references(columnOrTableDotColumn): this

Defined in: packages/orm/src/schema/ColumnDefinition.ts:436

Set the referenced column. Accepts either a bare column name ('id') or a 'table.column' shorthand that also sets the referenced table:

table.foreign('user_id').references('users.id').onDelete('CASCADE');
table.foreign('user_id').references('id').on('users').onDelete('CASCADE');

Parameters

columnOrTableDotColumn

string

"col" or "table.col".

Returns

this


on()

on(table): this

Defined in: packages/orm/src/schema/ColumnDefinition.ts:451

Set the referenced table.

Parameters

table

string

Returns

this


onDelete()

onDelete(action): this

Defined in: packages/orm/src/schema/ColumnDefinition.ts:460

Set the ON DELETE referential action.

Parameters

action

FKAction

Returns

this


onUpdate()

onUpdate(action): this

Defined in: packages/orm/src/schema/ColumnDefinition.ts:469

Set the ON UPDATE referential action.

Parameters

action

FKAction

Returns

this


cascadeOnDelete()

cascadeOnDelete(): this

Defined in: packages/orm/src/schema/ColumnDefinition.ts:478

Shorthand for .onDelete('CASCADE').

Returns

this


cascadeOnUpdate()

cascadeOnUpdate(): this

Defined in: packages/orm/src/schema/ColumnDefinition.ts:486

Shorthand for .onUpdate('CASCADE').

Returns

this


nullOnDelete()

nullOnDelete(): this

Defined in: packages/orm/src/schema/ColumnDefinition.ts:494

Shorthand for .onDelete('SET NULL').

Returns

this


restrictOnDelete()

restrictOnDelete(): this

Defined in: packages/orm/src/schema/ColumnDefinition.ts:502

Shorthand for .onDelete('RESTRICT').

Returns

this