kysely
    Preparing search index...

    Class ColumnDefinitionBuilder

    Implements

    Index

    Constructors

    Methods

    • 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.

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

      The generated SQL (MySQL):

      create table `person` (
      `id` integer primary key auto_increment
      )

      Returns ColumnDefinitionBuilder

    • Adds a default value constraint for the column.

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

      The generated SQL (MySQL):

      create table `pet` (
      `number_of_legs` integer default 4
      )

      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'

      await db.schema
      .createTable('pet')
      .addColumn(
      'created_at',
      'timestamp',
      (col) => col.defaultTo(sql`CURRENT_TIMESTAMP`)
      )
      .execute()

      The generated SQL (MySQL):

      create table `pet` (
      `created_at` timestamp default CURRENT_TIMESTAMP
      )

      Parameters

      • value: unknown

      Returns ColumnDefinitionBuilder

    • Makes the column a generated column using a generated always as statement.

      importsql } from 'kysely'

      await db.schema
      .createTable('person')
      .addColumn('full_name', 'varchar(255)',
      (col) => col.generatedAlwaysAs(sql`concat(first_name, ' ', last_name)`)
      )
      .execute()

      The generated SQL (MySQL):

      create table `person` (
      `full_name` varchar(255) generated always as (concat(first_name, ' ', last_name))
      )

      Parameters

      Returns ColumnDefinitionBuilder

    • 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.

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

      The generated SQL (PostgreSQL):

      create table "person" (
      "id" integer generated always as identity primary key
      )

      Returns ColumnDefinitionBuilder

    • 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.

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

      The generated SQL (PostgreSQL):

      create table "person" (
      "id" integer generated by default as identity primary key
      )

      Returns ColumnDefinitionBuilder

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

      import { sql } from 'kysely'

      await 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.

      import { sql } from 'kysely'

      await 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.

      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

    • 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.

      await db.schema
      .createTable('pet')
      .addColumn(
      'owner_id',
      'integer',
      (col) => col.references('person.id').onDelete('cascade')
      )
      .execute()

      The generated SQL (PostgreSQL):

      create table "pet" (
      "owner_id" integer references "person" ("id") on delete cascade
      )

      Parameters

      • onDelete: "no action" | "restrict" | "cascade" | "set null" | "set default"

      Returns ColumnDefinitionBuilder

    • 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.

      await db.schema
      .createTable('pet')
      .addColumn(
      'owner_id',
      'integer',
      (col) => col.references('person.id').onUpdate('cascade')
      )
      .execute()

      The generated SQL (PostgreSQL):

      create table "pet" (
      "owner_id" integer references "person" ("id") on update cascade
      )

      Parameters

      • onUpdate: "no action" | "restrict" | "cascade" | "set null" | "set default"

      Returns ColumnDefinitionBuilder

    • Makes a generated column stored instead of virtual. This method can only be used with generatedAlwaysAs

      import { sql } from 'kysely'

      await db.schema
      .createTable('person')
      .addColumn('full_name', 'varchar(255)', (col) => col
      .generatedAlwaysAs(sql`concat(first_name, ' ', last_name)`)
      .stored()
      )
      .execute()

      The generated SQL (MySQL):

      create table `person` (
      `full_name` varchar(255) generated always as (concat(first_name, ' ', last_name)) stored
      )

      Returns ColumnDefinitionBuilder