Skip to main content
The AlterTableBuilder class is used to construct ALTER TABLE queries. It provides a fluent API for modifying tables, adding or dropping columns, and managing constraints.

Methods

renameTo

Renames the table.
renameTo(newTableName: string): AlterTableExecutor
newTableName
string
required
The new name for the table
returns
AlterTableExecutor
An executor for the alter table query

setSchema

Sets the schema for the table.
setSchema(newSchema: string): AlterTableExecutor
newSchema
string
required
The new schema for the table
returns
AlterTableExecutor
An executor for the alter table query

alterColumn

Alters a column in the table.
alterColumn(
  column: string,
  alteration: AlterColumnBuilderCallback
): AlterTableColumnAlteringBuilder
column
string
required
The name of the column to alter
alteration
AlterColumnBuilderCallback
required
A callback that receives an AlterColumnBuilder and returns modifications
returns
AlterTableColumnAlteringBuilder
A builder for additional column alterations

dropColumn

Drops a column from the table.
dropColumn(column: string): AlterTableColumnAlteringBuilder
column
string
required
The name of the column to drop
returns
AlterTableColumnAlteringBuilder
A builder for additional column alterations

renameColumn

Renames a column.
renameColumn(
  column: string,
  newColumn: string
): AlterTableColumnAlteringBuilder
column
string
required
The current name of the column
newColumn
string
required
The new name for the column
returns
AlterTableColumnAlteringBuilder
A builder for additional column alterations

addColumn

Adds a new column to the table.
addColumn(
  columnName: string,
  dataType: DataTypeExpression,
  build?: ColumnDefinitionBuilderCallback
): AlterTableColumnAlteringBuilder
columnName
string
required
The name of the column to add
dataType
DataTypeExpression
required
The data type of the column
build
ColumnDefinitionBuilderCallback
Optional callback to configure the column definition
returns
AlterTableColumnAlteringBuilder
A builder for additional column alterations

modifyColumn

Creates an ALTER TABLE MODIFY COLUMN query. The MODIFY COLUMN statement is only implemented by MySQL and Oracle. On other databases you should use the alterColumn method.
modifyColumn(
  columnName: string,
  dataType: DataTypeExpression,
  build?: ColumnDefinitionBuilderCallback
): AlterTableColumnAlteringBuilder
columnName
string
required
The name of the column to modify
dataType
DataTypeExpression
required
The new data type for the column
build
ColumnDefinitionBuilderCallback
Optional callback to configure the column definition
returns
AlterTableColumnAlteringBuilder
A builder for additional column alterations

addUniqueConstraint

Adds a unique constraint for one or more columns. See CreateTableBuilder.addUniqueConstraint for more details.
addUniqueConstraint(
  constraintName: string,
  columns: string[],
  build?: UniqueConstraintNodeBuilderCallback
): AlterTableExecutor
constraintName
string
required
The name of the constraint
columns
string[]
required
The columns to include in the unique constraint
build
UniqueConstraintNodeBuilderCallback
Optional callback to configure the constraint
returns
AlterTableExecutor
An executor for the alter table query

addCheckConstraint

Adds a check constraint. See CreateTableBuilder.addCheckConstraint for more details.
addCheckConstraint(
  constraintName: string,
  checkExpression: Expression<any>,
  build?: CheckConstraintBuilderCallback
): AlterTableExecutor
constraintName
string
required
The name of the constraint
checkExpression
Expression<any>
required
The check expression
build
CheckConstraintBuilderCallback
Optional callback to configure the constraint
returns
AlterTableExecutor
An executor for the alter table query

addForeignKeyConstraint

Adds a foreign key constraint. Unlike CreateTableBuilder.addForeignKeyConstraint, this method returns the constraint builder and doesn’t take a callback as the last argument. This is because you can only add one column per ALTER TABLE query.
addForeignKeyConstraint(
  constraintName: string,
  columns: string[],
  targetTable: string,
  targetColumns: string[],
  build?: ForeignKeyConstraintBuilderCallback
): AlterTableAddForeignKeyConstraintBuilder
constraintName
string
required
The name of the constraint
columns
string[]
required
The columns in this table
targetTable
string
required
The target table name
targetColumns
string[]
required
The columns in the target table
build
ForeignKeyConstraintBuilderCallback
Optional callback to configure the constraint
returns
AlterTableAddForeignKeyConstraintBuilder
A builder for the foreign key constraint

addPrimaryKeyConstraint

Adds a primary key constraint for one or more columns. See CreateTableBuilder.addPrimaryKeyConstraint for more details.
addPrimaryKeyConstraint(
  constraintName: string,
  columns: string[],
  build?: PrimaryKeyConstraintBuilderCallback
): AlterTableExecutor
constraintName
string
required
The name of the constraint
columns
string[]
required
The columns to include in the primary key
build
PrimaryKeyConstraintBuilderCallback
Optional callback to configure the constraint
returns
AlterTableExecutor
An executor for the alter table query

dropConstraint

Drops a constraint from the table.
dropConstraint(constraintName: string): AlterTableDropConstraintBuilder
constraintName
string
required
The name of the constraint to drop
returns
AlterTableDropConstraintBuilder
A builder for dropping constraints

renameConstraint

Renames a constraint.
renameConstraint(
  oldName: string,
  newName: string
): AlterTableDropConstraintBuilder
oldName
string
required
The current name of the constraint
newName
string
required
The new name for the constraint
returns
AlterTableDropConstraintBuilder
A builder for the constraint operation

addIndex

Adds an index to the table.
addIndex(indexName: string): AlterTableAddIndexBuilder
indexName
string
required
The name of the index to add
returns
AlterTableAddIndexBuilder
A builder for creating the index

Examples

db.schema.alterTable('person')
  .addIndex('person_email_index')
  .column('email')
  .unique()
  .execute()
Generated SQL (MySQL):
alter table `person` add unique index `person_email_index` (`email`)

dropIndex

Drops an index from the table.
dropIndex(indexName: string): AlterTableExecutor
indexName
string
required
The name of the index to drop
returns
AlterTableExecutor
An executor for the alter table query

Examples

db.schema.alterTable('person')
  .dropIndex('person_email_index')
  .execute()
Generated SQL (MySQL):
alter table `person` drop index `test_first_name_index`

$call

Calls the given function passing this as the only argument. See CreateTableBuilder.$call for more details.
$call<T>(func: (qb: this) => T): T
func
(qb: this) => T
required
A function that receives the builder and returns a value
returns
T
The return value of the provided function

Build docs developers (and LLMs) love