# Query Runner API

> 英文标题：Query Runner API
> 官网原文：https://typeorm.io/docs/migrations/api
> 文档版本：TypeORM v1.1
> 体例：英文原文（EN）与中文译文（ZH）逐块并列，共 230 块

---

<!-- EN -->
# Query Runner API

<!-- ZH -->
# Query Runner API

<!-- EN -->
In order to use an API to change a database schema you can use `QueryRunner`.

<!-- ZH -->
要使用 API 来修改数据库结构，你可以使用 `QueryRunner`。

<!-- EN -->
```ts
import {
    MigrationInterface,
    QueryRunner,
    Table,
    TableIndex,
    TableColumn,
    TableForeignKey,
} from "typeorm"

export class QuestionRefactoringTIMESTAMP implements MigrationInterface {
    async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.createTable(
            new Table({
                name: "question",
                columns: [
                    {
                        name: "id",
                        type: "int",
                        isPrimary: true,
                    },
                    {
                        name: "name",
                        type: "varchar",
                    },
                ],
            }),
            true,
        )

        await queryRunner.createIndex(
            "question",
            new TableIndex({
                name: "IDX_QUESTION_NAME",
                columnNames: ["name"],
            }),
        )

        await queryRunner.createTable(
            new Table({
                name: "answer",
                columns: [
                    {
                        name: "id",
                        type: "int",
                        isPrimary: true,
                    },
                    {
                        name: "name",
                        type: "varchar",
                    },
                    {
                        name: "created_at",
                        type: "timestamp",
                        default: "now()",
                    },
                ],
            }),
            true,
        )

        await queryRunner.addColumn(
            "answer",
            new TableColumn({
                name: "questionId",
                type: "int",
            }),
        )

        await queryRunner.createForeignKey(
            "answer",
            new TableForeignKey({
                columnNames: ["questionId"],
                referencedColumnNames: ["id"],
                referencedTableName: "question",
                onDelete: "CASCADE",
            }),
        )
    }

    async down(queryRunner: QueryRunner): Promise<void> {
        const table = await queryRunner.getTable("answer")
        const foreignKey = table.foreignKeys.find(
            (fk) => fk.columnNames.indexOf("questionId") !== -1,
        )
        await queryRunner.dropForeignKey("answer", foreignKey)
        await queryRunner.dropColumn("answer", "questionId")
        await queryRunner.dropTable("answer")
        await queryRunner.dropIndex("question", "IDX_QUESTION_NAME")
        await queryRunner.dropTable("question")
    }
}
```

<!-- ZH -->
```ts
import {
    MigrationInterface,
    QueryRunner,
    Table,
    TableIndex,
    TableColumn,
    TableForeignKey,
} from "typeorm"

export class QuestionRefactoringTIMESTAMP implements MigrationInterface {
    async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.createTable(
            new Table({
                name: "question",
                columns: [
                    {
                        name: "id",
                        type: "int",
                        isPrimary: true,
                    },
                    {
                        name: "name",
                        type: "varchar",
                    },
                ],
            }),
            true,
        )

        await queryRunner.createIndex(
            "question",
            new TableIndex({
                name: "IDX_QUESTION_NAME",
                columnNames: ["name"],
            }),
        )

        await queryRunner.createTable(
            new Table({
                name: "answer",
                columns: [
                    {
                        name: "id",
                        type: "int",
                        isPrimary: true,
                    },
                    {
                        name: "name",
                        type: "varchar",
                    },
                    {
                        name: "created_at",
                        type: "timestamp",
                        default: "now()",
                    },
                ],
            }),
            true,
        )

        await queryRunner.addColumn(
            "answer",
            new TableColumn({
                name: "questionId",
                type: "int",
            }),
        )

        await queryRunner.createForeignKey(
            "answer",
            new TableForeignKey({
                columnNames: ["questionId"],
                referencedColumnNames: ["id"],
                referencedTableName: "question",
                onDelete: "CASCADE",
            }),
        )
    }

    async down(queryRunner: QueryRunner): Promise<void> {
        const table = await queryRunner.getTable("answer")
        const foreignKey = table.foreignKeys.find(
            (fk) => fk.columnNames.indexOf("questionId") !== -1,
        )
        await queryRunner.dropForeignKey("answer", foreignKey)
        await queryRunner.dropColumn("answer", "questionId")
        await queryRunner.dropTable("answer")
        await queryRunner.dropIndex("question", "IDX_QUESTION_NAME")
        await queryRunner.dropTable("question")
    }
}
```

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
getDatabases(): Promise<string[]>
```

<!-- ZH -->
```ts
getDatabases(): Promise<string[]>
```

<!-- EN -->
Returns all available database names including system databases.

<!-- ZH -->
返回所有可用的数据库名，包括系统数据库。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
getSchemas(database?: string): Promise<string[]>
```

<!-- ZH -->
```ts
getSchemas(database?: string): Promise<string[]>
```

<!-- EN -->
* `database` - If database parameter specified, returns schemas of that database

<!-- ZH -->
* `database` - 如果指定了 database 参数，返回该数据库下的模式

<!-- EN -->
Returns all available schema names including system schemas. Useful for SQLServer and Postgres only.

<!-- ZH -->
返回所有可用的模式名，包括系统模式。仅对 SQLServer 和 Postgres 有用。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
getTable(tableName: string): Promise<Table|undefined>
```

<!-- ZH -->
```ts
getTable(tableName: string): Promise<Table|undefined>
```

<!-- EN -->
* `tableName` - name of a table to be loaded

<!-- ZH -->
* `tableName` - 要加载的表名

<!-- EN -->
Loads a table by a given name from the database.

<!-- ZH -->
按给定名称从数据库加载一张表。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
getTables(tableNames: string[]): Promise<Table[]>
```

<!-- ZH -->
```ts
getTables(tableNames: string[]): Promise<Table[]>
```

<!-- EN -->
* `tableNames` - name of a tables to be loaded

<!-- ZH -->
* `tableNames` - 要加载的表名

<!-- EN -->
Loads a tables by a given names from the database.

<!-- ZH -->
按给定名称从数据库加载多张表。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
hasDatabase(database: string): Promise<boolean>
```

<!-- ZH -->
```ts
hasDatabase(database: string): Promise<boolean>
```

<!-- EN -->
* `database` - name of a database to be checked

<!-- ZH -->
* `database` - 要检查的数据库名

<!-- EN -->
Checks if database with the given name exist.

<!-- ZH -->
检查给定名称的数据库是否存在。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
hasSchema(schema: string): Promise<boolean>
```

<!-- ZH -->
```ts
hasSchema(schema: string): Promise<boolean>
```

<!-- EN -->
* `schema` - name of a schema to be checked

<!-- ZH -->
* `schema` - 要检查的模式名

<!-- EN -->
Checks if schema with the given name exist. Used only for SqlServer and Postgres.

<!-- ZH -->
检查给定名称的模式是否存在。仅用于 SqlServer 和 Postgres。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
hasTable(table: Table|string): Promise<boolean>
```

<!-- ZH -->
```ts
hasTable(table: Table|string): Promise<boolean>
```

<!-- EN -->
* `table` - Table object or name

<!-- ZH -->
* `table` - Table 对象或表名

<!-- EN -->
Checks if table exist.

<!-- ZH -->
检查表是否存在。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
hasColumn(table: Table|string, columnName: string): Promise<boolean>
```

<!-- ZH -->
```ts
hasColumn(table: Table|string, columnName: string): Promise<boolean>
```

<!-- EN -->
* `table` - Table object or name
* `columnName` - name of a column to be checked

<!-- ZH -->
* `table` - Table 对象或表名
* `columnName` - 要检查的列名

<!-- EN -->
Checks if column exist in the table.

<!-- ZH -->
检查表中是否存在该列。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createDatabase(database: string, ifNotExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
createDatabase(database: string, ifNotExists?: boolean): Promise<void>
```

<!-- EN -->
* `database` - database name
* `ifNotExists` - when set to `true`, silently ignores if the database already exists; otherwise throws an error (default)

<!-- ZH -->
* `database` - 数据库名
* `ifNotExists` - 设为 `true` 时，如果数据库已存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Creates a new database.

<!-- ZH -->
创建一个新数据库。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropDatabase(database: string, ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropDatabase(database: string, ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `database` - database name
* `ifExists` - when set to `true`, silently ignores if the database does not exist; otherwise throws an error (default)

<!-- ZH -->
* `database` - 数据库名
* `ifExists` - 设为 `true` 时，如果数据库不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops database.

<!-- ZH -->
删除数据库。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createSchema(schemaPath: string, ifNotExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
createSchema(schemaPath: string, ifNotExists?: boolean): Promise<void>
```

<!-- EN -->
* `schemaPath` - schema name. For SqlServer can accept schema path (e.g. 'dbName.schemaName') as parameter. If schema path passed, it will create schema in specified database
* `ifNotExists` - when set to `true`, silently ignores if the schema already exists; otherwise throws an error (default)

<!-- ZH -->
* `schemaPath` - 模式名。对 SqlServer 可以接受模式路径（例如 'dbName.schemaName'）作为参数。如果传入了模式路径，它会在指定数据库中创建模式
* `ifNotExists` - 设为 `true` 时，如果模式已存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Creates a new table schema.

<!-- ZH -->
创建一个新的表模式。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropSchema(schemaPath: string, ifExists?: boolean, isCascade?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropSchema(schemaPath: string, ifExists?: boolean, isCascade?: boolean): Promise<void>
```

<!-- EN -->
* `schemaPath` - schema name. For SqlServer can accept schema path (e.g. 'dbName.schemaName') as parameter. If schema path passed, it will drop schema in specified database
* `ifExists` - when set to `true`, silently ignores if the schema does not exist; otherwise throws an error (default)
* `isCascade` - If `true`, automatically drop objects (tables, functions, etc.) that are contained in the schema. Used only in Postgres.

<!-- ZH -->
* `schemaPath` - 模式名。对 SqlServer 可以接受模式路径（例如 'dbName.schemaName'）作为参数。如果传入了模式路径，它会在指定数据库中删除模式
* `ifExists` - 设为 `true` 时，如果模式不存在就静默忽略；否则抛出错误（默认）
* `isCascade` - 为 `true` 时，自动删除该模式中包含的对象（表、函数等）。仅用于 Postgres。

<!-- EN -->
Drops a table schema.

<!-- ZH -->
删除一个表模式。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createTable(table: Table, ifNotExists?: boolean, createForeignKeys?: boolean, createIndices?: boolean): Promise<void>
```

<!-- ZH -->
```ts
createTable(table: Table, ifNotExists?: boolean, createForeignKeys?: boolean, createIndices?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object.
* `ifNotExists` - when set to `true`, silently ignores if the table already exists; otherwise throws an error (default)
* `createForeignKeys` - indicates whether foreign keys will be created on table creation. Default `true`
* `createIndices` - indicates whether indexes will be created on table creation. Default `true`

<!-- ZH -->
* `table` - Table 对象。
* `ifNotExists` - 设为 `true` 时，如果表已存在就静默忽略；否则抛出错误（默认）
* `createForeignKeys` - 表示建表时是否创建外键。默认 `true`
* `createIndices` - 表示建表时是否创建索引。默认 `true`

<!-- EN -->
Creates a new table.

<!-- ZH -->
创建一张新表。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropTable(table: Table|string, ifExists?: boolean, dropForeignKeys?: boolean, dropIndices?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropTable(table: Table|string, ifExists?: boolean, dropForeignKeys?: boolean, dropIndices?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or table name to be dropped
* `ifExists` - when set to `true`, silently ignores if the table does not exist; otherwise throws an error (default)
* `dropForeignKeys` - indicates whether foreign keys will be dropped on table deletion. Default `true`
* `dropIndices` - indicates whether indexes will be dropped on table deletion. Default `true`

<!-- ZH -->
* `table` - 要删除的 Table 对象或表名
* `ifExists` - 设为 `true` 时，如果表不存在就静默忽略；否则抛出错误（默认）
* `dropForeignKeys` - 表示删表时是否删除外键。默认 `true`
* `dropIndices` - 表示删表时是否删除索引。默认 `true`

<!-- EN -->
Drops a table.

<!-- ZH -->
删除一张表。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createView(view: View, syncWithMetadata?: boolean, oldView?: View): Promise<void>
```

<!-- ZH -->
```ts
createView(view: View, syncWithMetadata?: boolean, oldView?: View): Promise<void>
```

<!-- EN -->
* `view` - View object
* `syncWithMetadata` - indicates whether to sync view with metadata (optional)
* `oldView` - old View object to be replaced (optional)

<!-- ZH -->
* `view` - View 对象
* `syncWithMetadata` - 表示是否让视图与元数据同步（可选）
* `oldView` - 要被替换的旧 View 对象（可选）

<!-- EN -->
Creates a new view.

<!-- ZH -->
创建一个新视图。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropView(view: View|string, ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropView(view: View|string, ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `view` - View object or view name to be dropped
* `ifExists` - when set to `true`, silently ignores if the view does not exist; otherwise throws an error (default)

<!-- ZH -->
* `view` - 要删除的 View 对象或视图名
* `ifExists` - 设为 `true` 时，如果视图不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops a view.

<!-- ZH -->
删除一个视图。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
renameTable(oldTableOrName: Table|string, newTableName: string): Promise<void>
```

<!-- ZH -->
```ts
renameTable(oldTableOrName: Table|string, newTableName: string): Promise<void>
```

<!-- EN -->
* `oldTableOrName` - old Table object or name to be renamed
* `newTableName` - new table name

<!-- ZH -->
* `oldTableOrName` - 要重命名的旧 Table 对象或表名
* `newTableName` - 新表名

<!-- EN -->
Renames a table.

<!-- ZH -->
重命名一张表。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
addColumn(table: Table|string, column: TableColumn): Promise<void>
```

<!-- ZH -->
```ts
addColumn(table: Table|string, column: TableColumn): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `column` - new column

<!-- ZH -->
* `table` - Table 对象或表名
* `column` - 新列

<!-- EN -->
Adds a new column.

<!-- ZH -->
添加一个新列。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
addColumns(table: Table|string, columns: TableColumn[]): Promise<void>
```

<!-- ZH -->
```ts
addColumns(table: Table|string, columns: TableColumn[]): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `columns` - new columns

<!-- ZH -->
* `table` - Table 对象或表名
* `columns` - 新列

<!-- EN -->
Adds a new column.

<!-- ZH -->
添加一个新列。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
renameColumn(table: Table|string, oldColumnOrName: TableColumn|string, newColumnOrName: TableColumn|string): Promise<void>
```

<!-- ZH -->
```ts
renameColumn(table: Table|string, oldColumnOrName: TableColumn|string, newColumnOrName: TableColumn|string): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `oldColumnOrName` - old column. Accepts TableColumn object or column name
* `newColumnOrName` - new column. Accepts TableColumn object or column name

<!-- ZH -->
* `table` - Table 对象或表名
* `oldColumnOrName` - 旧列。接受 TableColumn 对象或列名
* `newColumnOrName` - 新列。接受 TableColumn 对象或列名

<!-- EN -->
Renames a column.

<!-- ZH -->
重命名一个列。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
changeColumn(table: Table|string, oldColumn: TableColumn|string, newColumn: TableColumn): Promise<void>
```

<!-- ZH -->
```ts
changeColumn(table: Table|string, oldColumn: TableColumn|string, newColumn: TableColumn): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `oldColumn` - old column. Accepts TableColumn object or column name
* `newColumn` - new column. Accepts TableColumn object

<!-- ZH -->
* `table` - Table 对象或表名
* `oldColumn` - 旧列。接受 TableColumn 对象或列名
* `newColumn` - 新列。接受 TableColumn 对象

<!-- EN -->
Changes a column in the table.

<!-- ZH -->
修改表中的一个列。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
changeColumns(table: Table|string, changedColumns: { oldColumn: TableColumn, newColumn: TableColumn }[]): Promise<void>
```

<!-- ZH -->
```ts
changeColumns(table: Table|string, changedColumns: { oldColumn: TableColumn, newColumn: TableColumn }[]): Promise<void>
```

<!-- EN -->
* `table` - Table object or name

<!-- ZH -->
* `table` - Table 对象或表名

<!-- EN -->
* `changedColumns` - array of changed columns.

<!-- ZH -->
* `changedColumns` - 被修改列的数组。

<!-- EN -->
  <!-- -->

<!-- ZH -->
  <!-- -->

<!-- EN -->
  * `oldColumn` - old TableColumn object
  * `newColumn` - new TableColumn object

<!-- ZH -->
  * `oldColumn` - 旧 TableColumn 对象
  * `newColumn` - 新 TableColumn 对象

<!-- EN -->
Changes a columns in the table.

<!-- ZH -->
修改表中的多个列。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropColumn(table: Table|string, column: TableColumn|string, ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropColumn(table: Table|string, column: TableColumn|string, ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `column` - TableColumn object or column name to be dropped
* `ifExists` - when set to `true`, silently ignores if the column does not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `column` - 要删除的 TableColumn 对象或列名
* `ifExists` - 设为 `true` 时，如果列不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops a column in the table.

<!-- ZH -->
删除表中的一个列。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropColumns(table: Table|string, columns: TableColumn[]|string[], ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropColumns(table: Table|string, columns: TableColumn[]|string[], ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `columns` - array of TableColumn objects or column names to be dropped
* `ifExists` - when set to `true`, silently ignores if the columns do not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `columns` - 要删除的 TableColumn 对象或列名数组
* `ifExists` - 设为 `true` 时，如果这些列不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops columns in the table.

<!-- ZH -->
删除表中的多个列。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createPrimaryKey(table: Table|string, columnNames: string[]): Promise<void>
```

<!-- ZH -->
```ts
createPrimaryKey(table: Table|string, columnNames: string[]): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `columnNames` - array of column names which will be primary

<!-- ZH -->
* `table` - Table 对象或表名
* `columnNames` - 将作为主键的列名数组

<!-- EN -->
Creates a new primary key.

<!-- ZH -->
创建一个新主键。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
updatePrimaryKeys(table: Table|string, columns: TableColumn[]): Promise<void>
```

<!-- ZH -->
```ts
updatePrimaryKeys(table: Table|string, columns: TableColumn[]): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `columns` - array of TableColumn objects which will be updated

<!-- ZH -->
* `table` - Table 对象或表名
* `columns` - 将被更新的 TableColumn 对象数组

<!-- EN -->
Updates composite primary keys.

<!-- ZH -->
更新复合主键。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropPrimaryKey(table: Table|string, constraintName?: string, ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropPrimaryKey(table: Table|string, constraintName?: string, ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `constraintName` - constraint name (optional)
* `ifExists` - when set to `true`, silently ignores if the primary key does not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `constraintName` - 约束名（可选）
* `ifExists` - 设为 `true` 时，如果主键不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops a primary key.

<!-- ZH -->
删除一个主键。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createUniqueConstraint(table: Table|string, uniqueConstraint: TableUnique): Promise<void>
```

<!-- ZH -->
```ts
createUniqueConstraint(table: Table|string, uniqueConstraint: TableUnique): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `uniqueConstraint` - TableUnique object to be created

<!-- ZH -->
* `table` - Table 对象或表名
* `uniqueConstraint` - 要创建的 TableUnique 对象

<!-- EN -->
Creates new unique constraint.

<!-- ZH -->
创建新的唯一约束。

<!-- EN -->
> Note: does not work for MySQL, because MySQL stores unique constraints as unique indexes. Use `createIndex()` method instead.

<!-- ZH -->
> 注意：对 MySQL 无效，因为 MySQL 把唯一约束存储为唯一索引。请改用 `createIndex()` 方法。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createUniqueConstraints(table: Table|string, uniqueConstraints: TableUnique[]): Promise<void>
```

<!-- ZH -->
```ts
createUniqueConstraints(table: Table|string, uniqueConstraints: TableUnique[]): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `uniqueConstraints` - array of TableUnique objects to be created

<!-- ZH -->
* `table` - Table 对象或表名
* `uniqueConstraints` - 要创建的 TableUnique 对象数组

<!-- EN -->
Creates new unique constraints.

<!-- ZH -->
创建新的唯一约束。

<!-- EN -->
> Note: does not work for MySQL, because MySQL stores unique constraints as unique indexes. Use `createIndices()` method instead.

<!-- ZH -->
> 注意：对 MySQL 无效，因为 MySQL 把唯一约束存储为唯一索引。请改用 `createIndices()` 方法。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropUniqueConstraint(table: Table|string, uniqueOrName: TableUnique|string, ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropUniqueConstraint(table: Table|string, uniqueOrName: TableUnique|string, ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `uniqueOrName` - TableUnique object or unique constraint name to be dropped
* `ifExists` - when set to `true`, silently ignores if the constraint does not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `uniqueOrName` - 要删除的 TableUnique 对象或唯一约束名
* `ifExists` - 设为 `true` 时，如果约束不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops a unique constraint.

<!-- ZH -->
删除一个唯一约束。

<!-- EN -->
> Note: does not work for MySQL, because MySQL stores unique constraints as unique indexes. Use `dropIndex()` method instead.

<!-- ZH -->
> 注意：对 MySQL 无效，因为 MySQL 把唯一约束存储为唯一索引。请改用 `dropIndex()` 方法。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropUniqueConstraints(table: Table|string, uniqueConstraints: TableUnique[], ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropUniqueConstraints(table: Table|string, uniqueConstraints: TableUnique[], ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `uniqueConstraints` - array of TableUnique objects to be dropped
* `ifExists` - when set to `true`, silently ignores if the constraints do not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `uniqueConstraints` - 要删除的 TableUnique 对象数组
* `ifExists` - 设为 `true` 时，如果这些约束不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops unique constraints.

<!-- ZH -->
删除多个唯一约束。

<!-- EN -->
> Note: does not work for MySQL, because MySQL stores unique constraints as unique indexes. Use `dropIndices()` method instead.

<!-- ZH -->
> 注意：对 MySQL 无效，因为 MySQL 把唯一约束存储为唯一索引。请改用 `dropIndices()` 方法。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createCheckConstraint(table: Table|string, checkConstraint: TableCheck): Promise<void>
```

<!-- ZH -->
```ts
createCheckConstraint(table: Table|string, checkConstraint: TableCheck): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `checkConstraint` - TableCheck object

<!-- ZH -->
* `table` - Table 对象或表名
* `checkConstraint` - TableCheck 对象

<!-- EN -->
Creates a new check constraint.

<!-- ZH -->
创建一个新的检查约束。

<!-- EN -->
> Note: MySQL does not support check constraints.

<!-- ZH -->
> 注意：MySQL 不支持检查约束。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createCheckConstraints(table: Table|string, checkConstraints: TableCheck[]): Promise<void>
```

<!-- ZH -->
```ts
createCheckConstraints(table: Table|string, checkConstraints: TableCheck[]): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `checkConstraints` - array of TableCheck objects

<!-- ZH -->
* `table` - Table 对象或表名
* `checkConstraints` - TableCheck 对象数组

<!-- EN -->
Creates a new check constraint.

<!-- ZH -->
创建一个新的检查约束。

<!-- EN -->
> Note: MySQL does not support check constraints.

<!-- ZH -->
> 注意：MySQL 不支持检查约束。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropCheckConstraint(table: Table|string, checkOrName: TableCheck|string, ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropCheckConstraint(table: Table|string, checkOrName: TableCheck|string, ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `checkOrName` - TableCheck object or check constraint name
* `ifExists` - when set to `true`, silently ignores if the constraint does not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `checkOrName` - TableCheck 对象或检查约束名
* `ifExists` - 设为 `true` 时，如果约束不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops a check constraint.

<!-- ZH -->
删除一个检查约束。

<!-- EN -->
> Note: MySQL does not support check constraints.

<!-- ZH -->
> 注意：MySQL 不支持检查约束。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropCheckConstraints(table: Table|string, checkConstraints: TableCheck[], ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropCheckConstraints(table: Table|string, checkConstraints: TableCheck[], ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `checkConstraints` - array of TableCheck objects
* `ifExists` - when set to `true`, silently ignores if the constraints do not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `checkConstraints` - TableCheck 对象数组
* `ifExists` - 设为 `true` 时，如果这些约束不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops check constraints.

<!-- ZH -->
删除多个检查约束。

<!-- EN -->
> Note: MySQL does not support check constraints.

<!-- ZH -->
> 注意：MySQL 不支持检查约束。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createExclusionConstraint(table: Table|string, exclusionConstraint: TableExclusion): Promise<void>
```

<!-- ZH -->
```ts
createExclusionConstraint(table: Table|string, exclusionConstraint: TableExclusion): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `exclusionConstraint` - TableExclusion object

<!-- ZH -->
* `table` - Table 对象或表名
* `exclusionConstraint` - TableExclusion 对象

<!-- EN -->
Creates a new exclusion constraint.

<!-- ZH -->
创建一个新的排他约束。

<!-- EN -->
> Note: only PostgreSQL supports exclusion constraints.

<!-- ZH -->
> 注意：只有 PostgreSQL 支持排他约束。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createExclusionConstraints(table: Table|string, exclusionConstraints: TableExclusion[]): Promise<void>
```

<!-- ZH -->
```ts
createExclusionConstraints(table: Table|string, exclusionConstraints: TableExclusion[]): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `exclusionConstraints` - array of TableExclusion objects

<!-- ZH -->
* `table` - Table 对象或表名
* `exclusionConstraints` - TableExclusion 对象数组

<!-- EN -->
Creates new exclusion constraints.

<!-- ZH -->
创建新的排他约束。

<!-- EN -->
> Note: only PostgreSQL supports exclusion constraints.

<!-- ZH -->
> 注意：只有 PostgreSQL 支持排他约束。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropExclusionConstraint(table: Table|string, exclusionOrName: TableExclusion|string, ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropExclusionConstraint(table: Table|string, exclusionOrName: TableExclusion|string, ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `exclusionOrName` - TableExclusion object or exclusion constraint name
* `ifExists` - when set to `true`, silently ignores if the constraint does not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `exclusionOrName` - TableExclusion 对象或排他约束名
* `ifExists` - 设为 `true` 时，如果约束不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops an exclusion constraint.

<!-- ZH -->
删除一个排他约束。

<!-- EN -->
> Note: only PostgreSQL supports exclusion constraints.

<!-- ZH -->
> 注意：只有 PostgreSQL 支持排他约束。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropExclusionConstraints(table: Table|string, exclusionConstraints: TableExclusion[], ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropExclusionConstraints(table: Table|string, exclusionConstraints: TableExclusion[], ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `exclusionConstraints` - array of TableExclusion objects
* `ifExists` - when set to `true`, silently ignores if the constraints do not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `exclusionConstraints` - TableExclusion 对象数组
* `ifExists` - 设为 `true` 时，如果这些约束不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops exclusion constraints.

<!-- ZH -->
删除多个排他约束。

<!-- EN -->
> Note: only PostgreSQL supports exclusion constraints.

<!-- ZH -->
> 注意：只有 PostgreSQL 支持排他约束。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createForeignKey(table: Table|string, foreignKey: TableForeignKey): Promise<void>
```

<!-- ZH -->
```ts
createForeignKey(table: Table|string, foreignKey: TableForeignKey): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `foreignKey` - TableForeignKey object

<!-- ZH -->
* `table` - Table 对象或表名
* `foreignKey` - TableForeignKey 对象

<!-- EN -->
Creates a new foreign key.

<!-- ZH -->
创建一个新外键。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createForeignKeys(table: Table|string, foreignKeys: TableForeignKey[]): Promise<void>
```

<!-- ZH -->
```ts
createForeignKeys(table: Table|string, foreignKeys: TableForeignKey[]): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `foreignKeys` - array of TableForeignKey objects

<!-- ZH -->
* `table` - Table 对象或表名
* `foreignKeys` - TableForeignKey 对象数组

<!-- EN -->
Creates a new foreign keys.

<!-- ZH -->
创建新的外键。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropForeignKey(table: Table|string, foreignKeyOrName: TableForeignKey|string, ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropForeignKey(table: Table|string, foreignKeyOrName: TableForeignKey|string, ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `foreignKeyOrName` - TableForeignKey object or foreign key name
* `ifExists` - when set to `true`, silently ignores if the foreign key does not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `foreignKeyOrName` - TableForeignKey 对象或外键名
* `ifExists` - 设为 `true` 时，如果外键不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops a foreign key.

<!-- ZH -->
删除一个外键。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropForeignKeys(table: Table|string, foreignKeys: TableForeignKey[], ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropForeignKeys(table: Table|string, foreignKeys: TableForeignKey[], ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `foreignKeys` - array of TableForeignKey objects
* `ifExists` - when set to `true`, silently ignores if the foreign keys do not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `foreignKeys` - TableForeignKey 对象数组
* `ifExists` - 设为 `true` 时，如果这些外键不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops foreign keys.

<!-- ZH -->
删除多个外键。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createIndex(table: Table|string, index: TableIndex): Promise<void>
```

<!-- ZH -->
```ts
createIndex(table: Table|string, index: TableIndex): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `index` - TableIndex object

<!-- ZH -->
* `table` - Table 对象或表名
* `index` - TableIndex 对象

<!-- EN -->
Creates a new index.

<!-- ZH -->
创建一个新索引。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
createIndices(table: Table|string, indices: TableIndex[]): Promise<void>
```

<!-- ZH -->
```ts
createIndices(table: Table|string, indices: TableIndex[]): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `indices` - array of TableIndex objects

<!-- ZH -->
* `table` - Table 对象或表名
* `indices` - TableIndex 对象数组

<!-- EN -->
Creates new indexes.

<!-- ZH -->
创建新的索引。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropIndex(table: Table|string, index: TableIndex|string, ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropIndex(table: Table|string, index: TableIndex|string, ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `index` - TableIndex object or index name
* `ifExists` - when set to `true`, silently ignores if the index does not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `index` - TableIndex 对象或索引名
* `ifExists` - 设为 `true` 时，如果索引不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops an index.

<!-- ZH -->
删除一个索引。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
dropIndices(table: Table|string, indices: TableIndex[], ifExists?: boolean): Promise<void>
```

<!-- ZH -->
```ts
dropIndices(table: Table|string, indices: TableIndex[], ifExists?: boolean): Promise<void>
```

<!-- EN -->
* `table` - Table object or name
* `indices` - array of TableIndex objects
* `ifExists` - when set to `true`, silently ignores if the indexes do not exist; otherwise throws an error (default)

<!-- ZH -->
* `table` - Table 对象或表名
* `indices` - TableIndex 对象数组
* `ifExists` - 设为 `true` 时，如果这些索引不存在就静默忽略；否则抛出错误（默认）

<!-- EN -->
Drops indexes.

<!-- ZH -->
删除多个索引。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
clearTable(tableName: string, options?: {cascade?: boolean}): Promise<void>
```

<!-- ZH -->
```ts
clearTable(tableName: string, options?: {cascade?: boolean}): Promise<void>
```

<!-- EN -->
* `tableName` - table name
* `options` - additional options
  <!-- -->
  * `cascade` - Indicates whether to clear rows of tables that have foreign keys (supported by PostgreSQL/CockroachDB and Oracle only; other databases throw an error if set to `true`). Default `false`

<!-- ZH -->
* `tableName` - 表名
* `options` - 额外选项
  <!-- -->
  * `cascade` - 表示是否同时清空带外键的表中的行（仅 PostgreSQL/CockroachDB 和 Oracle 支持；其他数据库在设为 `true` 时会抛出错误）。默认 `false`

<!-- EN -->
Clears all table contents.

<!-- ZH -->
清空表中的所有内容。

<!-- EN -->
> Note: this operation uses SQL's TRUNCATE query which cannot be reverted in transactions.

<!-- ZH -->
> 注意：该操作使用 SQL 的 TRUNCATE 查询，无法在事务中回滚。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
enableSqlMemory(): void
```

<!-- ZH -->
```ts
enableSqlMemory(): void
```

<!-- EN -->
Enables special query runner mode in which sql queries won't be executed, instead they will be memorized into a special variable inside query runner. You can get memorized sql using `getMemorySql()` method.

<!-- ZH -->
启用 query runner 的特殊模式，在该模式下 sql 查询不会被执行，而是被记录到 query runner 内部的一个特殊变量中。你可以用 `getMemorySql()` 方法获取记录下来的 sql。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
disableSqlMemory(): void
```

<!-- ZH -->
```ts
disableSqlMemory(): void
```

<!-- EN -->
Disables special query runner mode in which sql queries won't be executed. Previously memorized sql will be flushed.

<!-- ZH -->
禁用 query runner 的特殊模式，在该模式下 sql 查询不会被执行。先前记录的 sql 会被清空。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
clearSqlMemory(): void
```

<!-- ZH -->
```ts
clearSqlMemory(): void
```

<!-- EN -->
Flushes all memorized sql statements.

<!-- ZH -->
清空所有已记录的 sql 语句。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
getMemorySql(): SqlInMemory
```

<!-- ZH -->
```ts
getMemorySql(): SqlInMemory
```

<!-- EN -->
* returns `SqlInMemory` object with array of `upQueries` and `downQueries` sql statements

<!-- ZH -->
* 返回 `SqlInMemory` 对象，其中包含 `upQueries` 和 `downQueries` 两个 sql 语句数组

<!-- EN -->
Gets sql stored in the memory. Parameters in the sql are already replaced.

<!-- ZH -->
获取内存中存储的 sql。sql 中的参数已经被替换过了。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
executeMemoryUpSql(): Promise<void>
```

<!-- ZH -->
```ts
executeMemoryUpSql(): Promise<void>
```

<!-- EN -->
Executes memorized up sql queries.

<!-- ZH -->
执行已记录的 up sql 查询。

<!-- EN -->
***

<!-- ZH -->
***

<!-- EN -->
```ts
executeMemoryDownSql(): Promise<void>
```

<!-- ZH -->
```ts
executeMemoryDownSql(): Promise<void>
```

<!-- EN -->
Executes memorized down sql queries.

<!-- ZH -->
执行已记录的 down sql 查询。

<!-- EN -->
***

<!-- ZH -->
***
