Skip to main content
Module

x/kysely_deno_postgres_dialect/mod.ts>kysely.ColumnDefinitionBuilder

Kysely dialect for PostgreSQL using the deno-postgres client.
Latest
class kysely.ColumnDefinitionBuilder
Re-export
import { kysely } from "https://deno.land/x/kysely_deno_postgres_dialect@v0.27.1/mod.ts";
const { ColumnDefinitionBuilder } = kysely;

Constructors

new
ColumnDefinitionBuilder(node: ColumnDefinitionNode)

Methods

$call<T>(func: (qb: this) => T): T

Simply calls the provided function passing this as the only argument. $call returns what the provided function returns.

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.

Adds a check constraint for the column.

Examples

import { sql } from 'kysely'

db.schema
  .createTable('pet')
  .addColumn('number_of_legs', 'integer', (col) =>
    col.check(sql`number_of_legs < 5`)
  )
  .execute()
defaultTo(value: DefaultValueExpression): ColumnDefinitionBuilder

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:

import { sql } from 'kysely'

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

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

Examples

import { sql } from 'kysely'

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

Adds the generated always as identity specifier on supported dialects.

Adds the generated by default as identity specifier on supported dialects.

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'
)

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
)

Adds a not null constraint for the column.

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
)

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.

Examples

col.references('person.id').onDelete('cascade')

Adds an on update constraint for the foreign key column.

Examples

col.references('person.id').onUpdate('cascade')

Makes the column the primary key.

If you want to specify a composite primary key use the CreateTableBuilder.addPrimaryKeyConstraint method.

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.

Examples

col.references('person.id')

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

Examples

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

Adds a unique constraint for the column.

Adds a unsigned modifier for the column.

This only works on some dialects like MySQL.