Class CreateTableBuilder<TB, C>

This builder can be used to create a create table query.

Type Parameters

  • TB extends string

  • C extends string = never

Hierarchy

  • CreateTableBuilder

Implements

Constructors

Methods

  • Calls the given function passing this as the only argument.

    Examples

    db.schema
    .createTable('test')
    .$call((builder) => builder.addColumn('id', 'integer'))
    .execute()
    const addDefaultColumns = <T extends string, C extends string = never>(
    builder: CreateTableBuilder<T, C>
    ) => {
    return builder
    .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()`)
    )
    }

    db.schema
    .createTable('test')
    .$call(addDefaultColumns)
    .execute()

    Type Parameters

    • T

    Parameters

    Returns T

  • Adds a column to the table.

    Examples

    importsql } 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 references statement in the column definition. Instead foreign key constraints need to be defined in the create table query. See the next example:

      .addColumn('parent_id', 'integer')
    .addForeignKeyConstraint(
    'person_parent_id_fk', ['parent_id'], 'person', ['id'],
    (cb) => cb.onDelete('cascade')
    )

    Another good example is that PostgreSQL doesn't support the auto_increment keyword and you need to define an autoincrementing column for example using serial:

    await db.schema
    .createTable('person')
    .addColumn('id', 'serial', (col) => col.primaryKey()),

    Type Parameters

    • CN extends string

    Parameters

    Returns CreateTableBuilder<TB, C | CN>

  • Adds a foreign key constraint.

    The constraint name can be anything you want, but it must be unique across the whole database.

    Examples

    addForeignKeyConstraint(
    'owner_id_foreign',
    ['owner_id'],
    'person',
    ['id'],
    )

    Add constraint for multiple columns:

    addForeignKeyConstraint(
    'owner_id_foreign',
    ['owner_id1', 'owner_id2'],
    'person',
    ['id1', 'id2'],
    (cb) => cb.onDelete('cascade')
    )

    Parameters

    Returns 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

    addPrimaryKeyConstraint('primary_key', ['first_name', 'last_name'])
    

    Parameters

    • constraintName: string
    • columns: C[]

    Returns 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

    addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'])
    

    In dialects such as PostgreSQL you can specify nulls not distinct as follows:

    addUniqueConstraint('first_name_last_name_unique', ['first_name', 'last_name'], (builder) => builder.nullsNotDistinct())
    

    Parameters

    • constraintName: string
    • columns: C[]
    • build: UniqueConstraintNodeBuilderCallback = noop

    Returns CreateTableBuilder<TB, C>

  • This can be used to add any additional SQL to the end of the query.

    Also see onCommit.

    Examples

    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_ci

    Parameters

    Returns CreateTableBuilder<TB, C>

  • This can be used to add any additional SQL to the front of the query after the create keyword.

    Also see temporary.

    Examples

    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

    Returns CreateTableBuilder<TB, C>

Generated using TypeDoc