Skip to main content
The ColumnDefinitionBuilder class is used to define column properties when creating or altering tables. It provides a fluent API for specifying constraints, defaults, and other column attributes.

Methods

autoIncrement

Adds auto_increment or autoincrement to the column definition depending on the dialect. Some dialects like PostgreSQL don’t support this. On PostgreSQL you can use the serial or bigserial data type instead.
returns
ColumnDefinitionBuilder
A new builder with auto increment enabled

Examples

Generated SQL (MySQL):

identity

Makes the column an identity column. This only works on some dialects like MS SQL Server (MSSQL). For PostgreSQL’s generated always as identity use generatedAlwaysAsIdentity.
returns
ColumnDefinitionBuilder
A new builder with identity enabled

Examples

Generated SQL (MSSQL):

primaryKey

Makes the column the primary key. If you want to specify a composite primary key use the CreateTableBuilder.addPrimaryKeyConstraint method.
returns
ColumnDefinitionBuilder
A new builder with primary key constraint

Examples

Generated SQL (MySQL):

references

Adds a foreign key constraint for the column. If your database engine doesn’t support foreign key constraints in the column definition (like MySQL 5) you need to call the table level CreateTableBuilder.addForeignKeyConstraint method instead.
ref
string
required
Reference in format table.column or schema.table.column
returns
ColumnDefinitionBuilder
A new builder with the foreign key reference

Examples

Generated SQL (PostgreSQL):

onDelete

Adds an ON DELETE constraint for the foreign key column. If your database engine doesn’t support foreign key constraints in the column definition (like MySQL 5) you need to call the table level CreateTableBuilder.addForeignKeyConstraint method instead.
onDelete
OnModifyForeignAction
required
The action to take on delete (e.g., cascade, set null, restrict, no action)
returns
ColumnDefinitionBuilder
A new builder with the on delete action

Examples

Generated SQL (PostgreSQL):

onUpdate

Adds an ON UPDATE constraint for the foreign key column. If your database engine doesn’t support foreign key constraints in the column definition (like MySQL 5) you need to call the table level CreateTableBuilder.addForeignKeyConstraint method instead.
onUpdate
OnModifyForeignAction
required
The action to take on update (e.g., cascade, set null, restrict, no action)
returns
ColumnDefinitionBuilder
A new builder with the on update action

Examples

Generated SQL (PostgreSQL):

unique

Adds a unique constraint for the column.
returns
ColumnDefinitionBuilder
A new builder with unique constraint

Examples

Generated SQL (MySQL):

notNull

Adds a NOT NULL constraint for the column.
returns
ColumnDefinitionBuilder
A new builder with not null constraint

Examples

Generated SQL (MySQL):

unsigned

Adds an UNSIGNED modifier for the column. This only works on some dialects like MySQL.
returns
ColumnDefinitionBuilder
A new builder with unsigned modifier

Examples

Generated SQL (MySQL):

defaultTo

Adds a default value constraint for the column.
value
DefaultValueExpression
required
The default value (can be a primitive value or a SQL expression)
returns
ColumnDefinitionBuilder
A new builder with default value

Examples

Generated SQL (MySQL):
Using SQL expressions:
Generated SQL (MySQL):

check

Adds a check constraint for the column.
expression
Expression<any>
required
The check expression
returns
ColumnDefinitionBuilder
A new builder with check constraint

Examples

Generated SQL (MySQL):

generatedAlwaysAs

Makes the column a generated column using a GENERATED ALWAYS AS statement.
expression
Expression<any>
required
The generation expression
returns
ColumnDefinitionBuilder
A new builder with generated column definition

Examples

Generated SQL (MySQL):

generatedAlwaysAsIdentity

Adds the GENERATED ALWAYS AS IDENTITY specifier. This only works on some dialects like PostgreSQL. For MS SQL Server (MSSQL)‘s identity column use identity.
returns
ColumnDefinitionBuilder
A new builder with generated always as identity

Examples

Generated SQL (PostgreSQL):

generatedByDefaultAsIdentity

Adds the GENERATED BY DEFAULT AS IDENTITY specifier on supported dialects. This only works on some dialects like PostgreSQL. For MS SQL Server (MSSQL)‘s identity column use identity.
returns
ColumnDefinitionBuilder
A new builder with generated by default as identity

Examples

Generated SQL (PostgreSQL):

stored

Makes a generated column stored instead of virtual. This method can only be used with generatedAlwaysAs.
returns
ColumnDefinitionBuilder
A new builder with stored modifier

Examples

Generated SQL (MySQL):

modifyFront

Adds any additional SQL right after the column’s data type.
modifier
Expression<any>
required
The SQL expression to add
returns
ColumnDefinitionBuilder
A new builder with the front modifier

Examples

Generated SQL (MySQL):

nullsNotDistinct

Adds NULLS NOT DISTINCT specifier. Should be used with unique constraint. This only works on some dialects like PostgreSQL.
returns
ColumnDefinitionBuilder
A new builder with nulls not distinct

Examples

Generated SQL (PostgreSQL):

ifNotExists

Adds IF NOT EXISTS specifier. This only works for PostgreSQL.
returns
ColumnDefinitionBuilder
A new builder with if not exists

Examples

Generated SQL (PostgreSQL):

modifyEnd

Adds any additional SQL to the end of the column definition.
modifier
Expression<any>
required
The SQL expression to add
returns
ColumnDefinitionBuilder
A new builder with the end modifier

Examples

Generated SQL (MySQL):

$call

Calls the provided function passing this as the only argument.
func
(qb: this) => T
required
A function that receives the builder and returns a value
returns
T
The return value of the provided function