Last Updated: 3/9/2026
- CreateTableBuilder
Class CreateTableBuilder
This builder can be used to create a create table query.
Type Parameters
- TB extends string
- C extends string = never
Implements
Index
Constructors
Methods
$call addCheckConstraint addColumn addForeignKeyConstraint addPrimaryKeyConstraint addUniqueConstraint as compile execute ifNotExists modifyEnd modifyFront onCommit temporary toOperationNode
Constructors
constructor
-
new CreateTableBuilder<TB extends string, C extends string = never>(
props: CreateTableBuilderProps,
): CreateTableBuilder<TB, C>Type Parameters
- TB extends string
- C extends string = never
Parameters
- props: CreateTableBuilderProps
Returns CreateTableBuilder<TB, C>
Methods
$call
-
$call<T>(func: (qb: this) => T): T
Calls the given function passing
thisas the only argument.Examples
await db. schema . createTable('test') . $call((builder) => builder. addColumn('id', 'integer')) . execute()This is useful for creating reusable functions that can be called with a builder.
import { type CreateTableBuilder, sql } from 'kysely' const addDefaultColumns = (ctb: CreateTableBuilder< any, any>) => { return ctb . addColumn('id', 'integer', (col) => col. notNull()) . addColumn('created_at', 'date', (col) => col. notNull(). defaultTo(sql`now()`) ) . addColumn('updated_at', 'date', (col) => col. notNull(). defaultTo(sql`now()`) )} await db. schema . createTable('test') . $call(addDefaultColumns) . execute()Type Parameters
- T
Parameters
- func: (qb: this) => T
Returns T
addCheckConstraint
-
addCheckConstraint(
constraintName: string,
checkExpression: Expression,
build?: CheckConstraintBuilderCallback,
): CreateTableBuilder<TB, C>Adds a check constraint.
The constraint name can be anything you want, but it must be unique across the whole database.
Examples
import { sql } from 'kysely' await db. schema . createTable('animal') . addColumn('number_of_legs', 'integer') . addCheckConstraint('check_legs', sql`number_of_legs < 5`) . execute()Parameters
- constraintName: string
- checkExpression: Expression
- build: CheckConstraintBuilderCallback = noop
Returns CreateTableBuilder<TB, C>
addColumn
-
addColumn<CN extends string>(
columnName: CN,
dataType: DataTypeExpression,
build?: ColumnBuilderCallback,
): CreateTableBuilder<TB, C | CN>Adds a column to the table.
Examples
import { sql } from 'kysely' await db. schema . createTable('person') . addColumn('id', 'integer', (col) => col. autoIncrement(). primaryKey()) . addColumn('first_name', 'varchar(50)', (col) => col. notNull()) . addColumn('last_name', 'varchar(255)') . addColumn('bank_balance', 'numeric(8, 2)') // You can specify any data type using the `sql` tag if the types // don't include it. . addColumn('data', sql `any_type_here`) . addColumn('parent_id', 'integer', (col) => col. references('person.id'). onDelete('cascade') )With this method, it’s once again good to remember that Kysely just builds the query and doesn’t provide the same API for all databases. For example, some databases like older MySQL don’t support the
referencesstatement in the column definition. Instead foreign key constraints need to be defined in thecreate tablequery. See the next example:await db. schema . createTable('person') . addColumn('id', 'integer', (col) => col. primaryKey()) . addColumn('parent_id', 'integer') . addForeignKeyConstraint( 'person_parent_id_fk', ['parent_id'], 'person', ['id'], (cb) => cb. onDelete('cascade') ) . execute()Another good example is that PostgreSQL doesn’t support the
auto_incrementkeyword and you need to define an autoincrementing column for example usingserial:await db. schema . createTable('person') . addColumn('id', 'serial', (col) => col. primaryKey()) . execute()Type Parameters
- CN extends string
Parameters
- columnName: CN
- dataType: DataTypeExpression
- build: ColumnBuilderCallback = noop
Returns CreateTableBuilder<TB, C | CN>
addForeignKeyConstraint
-
addForeignKeyConstraint(
constraintName: string,
columns: C[],
targetTable: string,
targetColumns: string[],
build?: ForeignKeyConstraintBuilderCallback,
): CreateTableBuilder<TB, C>Adds a foreign key constraint.
The constraint name can be anything you want, but it must be unique across the whole database.
Examples
await db. schema . createTable('pet') . addColumn('owner_id', 'integer') . addForeignKeyConstraint( 'owner_id_foreign', ['owner_id'], 'person', ['id'], ) . execute()Add constraint for multiple columns:
await db. schema . createTable('pet') . addColumn('owner_id1', 'integer') . addColumn('owner_id2', 'integer') . addForeignKeyConstraint( 'owner_id_foreign', ['owner_id1', 'owner_id2'], 'person', ['id1', 'id2'], (cb) => cb. onDelete('cascade') ) . execute()Parameters
- constraintName: string
- columns: C[]
- targetTable: string
- targetColumns: string[]
- build: ForeignKeyConstraintBuilderCallback = noop
Returns CreateTableBuilder<TB, C>
addPrimaryKeyConstraint
-
addPrimaryKeyConstraint(
constraintName: string,
columns: C[],
build?: PrimaryKeyConstraintBuilderCallback,
): CreateTableBuilder<TB, C>Adds a primary key constraint for one or more columns.
The constraint name can be anything you want, but it must be unique across the whole database.
Examples
await db. schema . createTable('person') . addColumn('first_name', 'varchar(64)') . addColumn('last_name', 'varchar(64)') . addPrimaryKeyConstraint('primary_key', ['first_name', 'last_name']) . execute()Parameters
- constraintName: string
- columns: C[]
- build: PrimaryKeyConstraintBuilderCallback = noop
Returns CreateTableBuilder<TB, C>
addUniqueConstraint
-
addUniqueConstraint(
constraintName: string,
columns: C[],
build?: UniqueConstraintNodeBuilderCallback,
): CreateTableBuilder<TB, C>Adds a unique constraint for one or more columns.
The constraint name can be anything you want, but it must be unique across the whole database.
Examples
await db. schema . createTable('person') . addColumn('first_name', 'varchar(64)') . addColumn('last_name', 'varchar(64)') . addUniqueConstraint( 'first_name_last_name_unique', ['first_name', 'last_name'] ) . execute()In dialects such as PostgreSQL you can specify
nulls not distinctas follows:await db. schema . createTable('person') . addColumn('first_name', 'varchar(64)') . addColumn('last_name', 'varchar(64)') . addUniqueConstraint( 'first_name_last_name_unique', ['first_name', 'last_name'], (cb) => cb. nullsNotDistinct() ) . execute()Parameters
- constraintName: string
- columns: C[]
- build: UniqueConstraintNodeBuilderCallback = noop
Returns CreateTableBuilder<TB, C>
as
-
as(expression: Expression
): CreateTableBuilder<string, never> Allows to create table from
selectquery.Examples
await db. schema . createTable('copy') . temporary() . as(db. selectFrom('person'). select(['first_name', 'last_name'])) . execute()The generated SQL (PostgreSQL):
create temporary table "copy" as select "first_name", "last_name" from "person"Parameters
- expression: Expression
Returns CreateTableBuilder<string, never>
- expression: Expression
compile
-
compile(): CompiledQuery
Returns CompiledQuery
execute
ifNotExists
-
ifNotExists(): CreateTableBuilder<TB, C>
Adds the “if not exists” modifier.
If the table already exists, no error is thrown if this method has been called.
Returns CreateTableBuilder<TB, C>
modifyEnd
-
modifyEnd(modifier: Expression
): CreateTableBuilder<TB, C> This can be used to add any additional SQL to the end of the query.
Also see onCommit.
Examples
import { sql } from 'kysely' await db. schema . createTable('person') . addColumn('id', 'integer', col => col. primaryKey()) . addColumn('first_name', 'varchar(64)', col => col. notNull()) . addColumn('last_name', 'varchar(64)', col => col. notNull()) . modifyEnd(sql `collate utf8_unicode_ci`) . execute()The generated SQL (MySQL):
create table `person` ( `id` integer primary key, `first_name` varchar(64) not null, `last_name` varchar(64) not null ) collate utf8_unicode_ciParameters
- modifier: Expression
Returns CreateTableBuilder<TB, C>
- modifier: Expression
modifyFront
-
modifyFront(modifier: Expression
): CreateTableBuilder<TB, C> This can be used to add any additional SQL to the front of the query after the
createkeyword.Also see temporary.
Examples
import { sql } from 'kysely' await db. schema . createTable('person') . modifyFront(sql `global temporary`) . addColumn('id', 'integer', col => col. primaryKey()) . addColumn('first_name', 'varchar(64)', col => col. notNull()) . addColumn('last_name', 'varchar(64)', col => col. notNull()) . execute()The generated SQL (Postgres):
create global temporary table "person" ( "id" integer primary key, "first_name" varchar(64) not null, "last_name" varchar(64) not null )Parameters
- modifier: Expression
Returns CreateTableBuilder<TB, C>
- modifier: Expression
onCommit
-
onCommit(onCommit: string): CreateTableBuilder<TB, C>
Adds an “on commit” statement.
This can be used in conjunction with temporary tables on supported databases like PostgreSQL.
Parameters
- onCommit: string
Returns CreateTableBuilder<TB, C>
temporary
-
temporary(): CreateTableBuilder<TB, C>
Adds the “temporary” modifier.
Use this to create a temporary table.
Returns CreateTableBuilder<TB, C>
toOperationNode
-
toOperationNode(): CreateTableNode
Returns CreateTableNode
Settings
Member Visibility
On This Page
Constructors
Methods