Skip to content

database.alterTable()

Oxford Harrison edited this page Nov 15, 2024 · 7 revisions

DOCSAPIDatabase API


Programmatically perform an ALTER TABLE operation.

See related ➞ ALTER TABLE

Syntax

database.alterTable(
    alterSpec: string,
    callback: (tableSchema: TableSchema) => void,
    options?: AlterOptions
): Promise<AlterTableResult>;
Param Interfaces Description
alterSpec - A table name.
callback TableSchema A callback function that recieves the requested schema. This can be async.
options? AlterOptions Optional extra parameters for the query.

AlterOptions

type AlterOptions = {
    cascadeRule?: boolean;
    existsChecks?: boolean;
} & QueryOptions;
Param Interfaces Description
cascadeRule - A flag that adds CASCADE flags to subtree manipulations. Defaults to false. (See ➞ ALTER TABLE ➞ Managing Columns)
existsChecks - A flag that adds EXISTS checks to subtree manipulations. Defaults to false. (See ➞ ALTER TABLE ➞ Managing columns)
Interface Description
QueryOptions Inherited options.

AlterTableResult

type AlterTableResult = Table | TableSchema | Savepoint | null;
Type Interfaces Description
Table Table For an ALTER operation without a RETURNING clause—an instance of Table on the just created object.
TableSchema TableSchema For an operation with a RETURNING clause set to SCHEMA-the resulting database schema instance.
Savepoint | null Savepoint For an operation with a RETURNING clause set to SAVEPOINT-the Savepoint instance associated with the DDL operation; null when savepoint creation has been disabled at the server level.

Usage

See examples ➞ ALTER TABLE

Basic call pattern

Change table name:

// Change table name
await database.alterTable(
    'table_1',
    (schema) => {
        schema.name('table_1_new');
    },
    { desc: 'Alter description' }
);
Clone this wiki locally