Class ColumnDefinitionBuilder

Hierarchy

  • ColumnDefinitionBuilder

Implements

Constructors

Methods

  • Adds a default value constraint for the column.

    Examples

    db.schema
    .createTable('pet')
    .addColumn('number_of_legs', 'integer', (col) => col.defaultTo(4))
    .execute()

    Values passed to defaultTo are interpreted as value literals by default. You can define an arbitrary SQL expression using the sql template tag:

    importsql } from 'kysely'

    db.schema
    .createTable('pet')
    .addColumn(
    'number_of_legs',
    'integer',
    (col) => col.defaultTo(sql`any SQL here`)
    )
    .execute()

    Parameters

    • value: unknown

    Returns ColumnDefinitionBuilder

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

    Examples

    db.schema.createTable('person')
    .addColumn('id', 'integer', col => col.primaryKey())
    .addColumn('age', 'integer', col => col.unsigned().notNull().modifyEnd(sql`comment ${sql.lit('it is not polite to ask a woman her age')}`))
    .execute()

    The generated SQL (MySQL):

    create table `person` (
    `id` integer primary key,
    `age` integer unsigned not null comment 'it is not polite to ask a woman her age'
    )

    Parameters

    Returns ColumnDefinitionBuilder

  • This can be used to add any additional SQL right after the column's data type.

    Examples

    db.schema.createTable('person')
    .addColumn('id', 'integer', col => col.primaryKey())
    .addColumn('first_name', 'varchar(36)', col => col.modifyFront(sql`collate utf8mb4_general_ci`).notNull())
    .execute()

    The generated SQL (MySQL):

    create table `person` (
    `id` integer primary key,
    `first_name` varchar(36) collate utf8mb4_general_ci not null
    )

    Parameters

    Returns ColumnDefinitionBuilder

  • Adds nulls not distinct specifier. Should be used with unique constraint.

    This only works on some dialects like PostgreSQL.

    Examples

    db.schema.createTable('person')
    .addColumn('id', 'integer', col => col.primaryKey())
    .addColumn('first_name', 'varchar(30)', col => col.unique().nullsNotDistinct())
    .execute()

    The generated SQL (PostgreSQL):

    create table "person" (
    "id" integer primary key,
    "first_name" varchar(30) unique nulls not distinct
    )

    Returns ColumnDefinitionBuilder

Generated using TypeDoc