Module

x/kysely_postgrs_js_dialect/deps.ts>kysely.CreateTableBuilder#addColumn

Kysely dialect for PostgreSQL using the Postgres.js client.
method kysely.CreateTableBuilder.prototype.addColumn
import { kysely } from "https://dotland.deno.dev/x/kysely_postgrs_js_dialect@v0.27.3/deps.ts";
const { CreateTableBuilder } = kysely;

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

columnName: CN
dataType: DataTypeExpression
optional
build: ColumnBuilderCallback