Skip to content

postgrelize / QueryInterface

Class: QueryInterface ​

Defined in: src/query-interface/query-interface.ts:38

Schema manipulation API (Sequelize v6 QueryInterface) for PostgreSQL. Used by sync() and by migrations (sequelize-cli, umzug). Obtain the instance with Sequelize.getQueryInterface. Table names are string or { tableName, schema }; identifiers are always quoted and values bound or escaped, never concatenated raw.

Example ​

ts
const queryInterface = sequelize.getQueryInterface();
await queryInterface.createTable('users', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  name: { type: DataTypes.STRING, allowNull: false },
});
await queryInterface.addIndex('users', ['name'], { unique: true });

See ​

Schema guide

Extends ​

  • QueryInterfaceCatalog

Constructors ​

Constructor ​

new QueryInterface(sequelize): QueryInterface

Defined in: src/query-interface/query-interface.ts:57

Creates the schema API bound to a Sequelize instance; prefer Sequelize.getQueryInterface.

Parameters ​

sequelize ​

RuntimeOwner

Owning Sequelize instance.

Returns ​

QueryInterface

Example ​

ts
const api = new QueryInterface(sequelize);
await api.showAllTables();

Overrides ​

QueryInterfaceCatalog.constructor

Properties ​

queryGenerator ​

readonly queryGenerator: QueryGenerator

Defined in: src/query-interface/query-interface.ts:40

Sequelize v6 queryGenerator property; only selectQuery is provided.


sequelize ​

readonly sequelize: RuntimeOwner

Defined in: src/query-interface/query-interface-tables.ts:19

Owning Sequelize instance.

Inherited from ​

QueryInterfaceCatalog.sequelize

Methods ​

addColumn() ​

addColumn(table, column, attribute, options?): Promise<void>

Defined in: src/query-interface/query-interface.ts:79

Adds a column with ALTER TABLE ... ADD COLUMN; a plain data type or a full attribute definition (nullability, default, references, ...) is accepted.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

column ​

string

Column name.

attribute ​

DataTypeInput | Readonly<{ allowNull?: boolean; autoIncrement?: boolean; comment?: string; defaultValue?: unknown; field?: string; generated?: Readonly<{ as: unknown; stored?: boolean; }>; identity?: "always" | "byDefault"; onDelete?: string; onUpdate?: string; primaryKey?: boolean; references?: string | Readonly<{ deferrable?: Readonly<{ sql: string; }>; key?: string; model: string | Readonly<{ schema?: string; tableName: string; }> | Readonly<{ getTableName(): string | Readonly<...>; }>; }>; type: DataTypeInput; unique?: string | boolean | Readonly<{ msg?: string; name?: string; nullsNotDistinct?: boolean; }>; validate?: Readonly<Record<string, unknown> & object>; values?: readonly string[]; get?(this): unknown; set?(this, value): void; }>

Column definition.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.addColumn('users', 'nickname', { type: DataTypes.STRING, allowNull: true });

addConstraint() ​

addConstraint(table, options): Promise<void>

Defined in: src/query-interface/query-interface.ts:219

Adds a CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY or EXCLUDE constraint to a table. notValid: true skips checking existing rows (check and foreign key); validate later with QueryInterface.validateConstraint.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

options ​

Readonly<{ deferrable?: Readonly<{ sql: string; }>; enforced?: boolean; exclude?: readonly ExcludeElement[]; fields?: readonly string[]; include?: readonly string[]; name?: string; notValid?: boolean; nullsNotDistinct?: boolean; onDelete?: string; onUpdate?: string; period?: boolean; references?: Readonly<{ field: string; fields?: readonly string[]; table: TableName; }>; type: "unique" | "check" | "primary key" | "foreign key" | "exclude" | "CHECK" | "UNIQUE" | "PRIMARY KEY" | "FOREIGN KEY" | "EXCLUDE"; using?: string; where?: unknown; withoutOverlaps?: boolean; }> & QueryOptionsBase

Constraint definition.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.addConstraint('products', {
  type: 'check',
  fields: ['price'],
  name: 'products_price_positive',
  where: { price: { [Op.gt]: 0 } },
});
await queryInterface.addConstraint('bookings', {
  type: 'exclude',
  name: 'bookings_no_overlap',
  using: 'gist',
  exclude: [
    { name: 'room', with: '=' },
    { expression: 'tstzrange(starts, ends)', with: '&&' },
  ],
});

addIndex() ​

addIndex(table, fieldsOrOptions, options?): Promise<void>

Defined in: src/query-interface/query-interface.ts:164

Creates an index on a table, optionally unique, partial, covering or CONCURRENTLY.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

fieldsOrOptions ​

readonly IndexField[] | Readonly<{ concurrently?: boolean; fields?: readonly IndexField[]; ifNotExists?: boolean; include?: readonly string[]; name?: string; nullsNotDistinct?: boolean; operator?: string; unique?: boolean; using?: string; where?: unknown; }>

Indexed fields or index options.

options? ​

Readonly<{ concurrently?: boolean; fields?: readonly IndexField[]; ifNotExists?: boolean; include?: readonly string[]; name?: string; nullsNotDistinct?: boolean; operator?: string; unique?: boolean; using?: string; where?: unknown; }> & QueryOptionsBase = {}

Index options when fields are given separately.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.addIndex('users', ['email'], { unique: true, name: 'users_email_unique' });
await queryInterface.addIndex('users', {
  name: 'users_lower_email',
  fields: [{ expression: 'lower(email)', nulls: 'LAST' }],
});

alterPolicy() ​

alterPolicy(table, name, options?): Promise<void>

Defined in: src/query-interface/query-interface-policies.ts:109

postgrelize extension

Changes a policy with ALTER POLICY: roles, conditions and name (rename, applied last).

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

name ​

string

Current policy name.

options? ​

Readonly<Pick<Readonly<{ command?: "all" | "update" | "delete" | "insert" | "select"; kind?: "permissive" | "restrictive"; roles?: readonly string[]; using?: PolicyCondition; withCheck?: PolicyCondition; }>, "using" | "roles" | "withCheck"> & object> & QueryOptionsBase = {}

Changes (at least one) and query options.

Returns ​

Promise<void>

Throws ​

Error when no change is given.

Example ​

ts
await queryInterface.alterPolicy('documents', 'tenant_isolation', { roles: ['app_user', 'app_admin'] });

Inherited from ​

QueryInterfaceCatalog.alterPolicy


alterSequence() ​

alterSequence(name, options?): Promise<void>

Defined in: src/query-interface/query-interface-objects.ts:102

postgrelize extension

Changes a sequence with ALTER SEQUENCE.

Parameters ​

name ​

TableName

Sequence name or { tableName, schema }.

options? ​

Omit<Readonly<{ cache?: SequenceNumber; cycle?: boolean; dataType?: "bigint" | "integer" | "smallint"; ifNotExists?: boolean; increment?: SequenceNumber; maxValue?: false | SequenceNumber; minValue?: false | SequenceNumber; ownedBy?: Readonly<{ column: string; table: TableName; }> | null; start?: SequenceNumber; }>, "ifNotExists" | "dataType" | "start"> & Readonly<{ ifExists?: boolean; restartWith?: true | SequenceNumber; }> & QueryOptionsBase = {}

Properties to change (at least one) and query options.

Returns ​

Promise<void>

Throws ​

Error when no property is given or a number is not whole.

Example ​

ts
await queryInterface.alterSequence('invoice_number', { restartWith: 5000, cycle: false });

Inherited from ​

QueryInterfaceCatalog.alterSequence


assertTableHasColumn() ​

assertTableHasColumn(table, column, options?): Promise<Record<string, Readonly<{ allowNull: boolean; comment: string | null; defaultValue: unknown; primaryKey: boolean; special: readonly string[]; type: string; }>>>

Defined in: src/query-interface/query-interface-catalog.ts:295

Describes a table and throws when it lacks a column.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

column ​

string

Column name.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<Record<string, Readonly<{ allowNull: boolean; comment: string | null; defaultValue: unknown; primaryKey: boolean; special: readonly string[]; type: string; }>>>

The table description (see QueryInterface.describeTable).

Throws ​

Error when the table has no such column.

Example ​

ts
const columns = await queryInterface.assertTableHasColumn('users', 'email');

Inherited from ​

QueryInterfaceCatalog.assertTableHasColumn


attachPartition() ​

attachPartition(parent, partition, bound, options?): Promise<void>

Defined in: src/query-interface/query-interface-partitions.ts:70

postgrelize extension

Attaches an existing table as a partition (ALTER TABLE ... ATTACH PARTITION). PostgreSQL scans the table to check the bounds unless a matching CHECK constraint exists.

Parameters ​

parent ​

TableName

Partitioned table name or { tableName, schema }.

partition ​

TableName

Existing table name or { tableName, schema }.

bound ​

PartitionBoundSpec

Partition bounds, see QueryInterface.createPartition.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.attachPartition('events', 'events_2027', {
  from: ['2027-01-01'],
  to: ['2028-01-01'],
});

Inherited from ​

QueryInterfaceCatalog.attachPartition


bulkDelete() ​

bulkDelete(table, where?, options?): Promise<number>

Defined in: src/query-interface/query-interface.ts:371

Deletes every row matching a where object with one DELETE, without model hooks.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

where? ​

unknown = {}

Filter (where object keyed by column); empty deletes all rows.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<number>

Number of deleted rows.

Example ​

ts
const removed = await queryInterface.bulkDelete('users', { active: false });

bulkInsert() ​

bulkInsert(table, records, options?): Promise<number>

Defined in: src/query-interface/query-interface.ts:327

Inserts several rows with one INSERT (used by seeders). Values are bound as parameters.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

records ​

readonly Readonly<Record<string, unknown>>[]

Rows keyed by column name.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<number>

Number of inserted rows.

Example ​

ts
const inserted = await queryInterface.bulkInsert('users', [
  { name: 'Ada', email: 'ada@example.com' },
  { name: 'Alan', email: 'alan@example.com' },
]);

bulkUpdate() ​

bulkUpdate(table, values, where, options?): Promise<number>

Defined in: src/query-interface/query-interface.ts:349

Updates every row matching a where object with one UPDATE, without model hooks or validation.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

values ​

Readonly<Record<string, unknown>>

New column values.

where ​

unknown

Filter (where object keyed by column).

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<number>

Number of updated rows.

Example ​

ts
const updated = await queryInterface.bulkUpdate('users', { active: false }, { name: 'Ada' });

changeColumn() ​

changeColumn(table, column, attribute, options?): Promise<void>

Defined in: src/query-interface/query-interface.ts:117

Alters the type, nullability, default and comment of an existing column in one go.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

column ​

string

Column name.

attribute ​

DataTypeInput | Readonly<{ allowNull?: boolean; autoIncrement?: boolean; comment?: string; defaultValue?: unknown; field?: string; generated?: Readonly<{ as: unknown; stored?: boolean; }>; identity?: "always" | "byDefault"; onDelete?: string; onUpdate?: string; primaryKey?: boolean; references?: string | Readonly<{ deferrable?: Readonly<{ sql: string; }>; key?: string; model: string | Readonly<{ schema?: string; tableName: string; }> | Readonly<{ getTableName(): string | Readonly<...>; }>; }>; type: DataTypeInput; unique?: string | boolean | Readonly<{ msg?: string; name?: string; nullsNotDistinct?: boolean; }>; validate?: Readonly<Record<string, unknown> & object>; values?: readonly string[]; get?(this): unknown; set?(this, value): void; }>

New column definition.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.changeColumn('users', 'name', { type: DataTypes.STRING(100), allowNull: false });

createDatabase() ​

createDatabase(database, options?): Promise<void>

Defined in: src/query-interface/query-interface-catalog.ts:232

Runs CREATE DATABASE. Must run outside a transaction.

Parameters ​

database ​

string

Database name (quoted).

options? ​

CreateDatabaseOptions = {}

encoding, collate, ctype, template (escaped as literals) and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.createDatabase('tenant_42', { encoding: 'UTF8', template: 'template0' });

Inherited from ​

QueryInterfaceCatalog.createDatabase


createExtension() ​

createExtension(name, options?): Promise<void>

Defined in: src/query-interface/query-interface-objects.ts:42

postgrelize extension

Installs an extension with CREATE EXTENSION. Needs the privileges PostgreSQL requires for that extension (superuser or a trusted extension).

Parameters ​

name ​

string

Extension name, quoted as an identifier.

options? ​

Readonly<{ cascade?: boolean; ifNotExists?: boolean; schema?: string; version?: string; }> & QueryOptionsBase = {}

ifNotExists, schema, version, cascade and query options.

Returns ​

Promise<void>

Throws ​

DatabaseError when the extension is not available on the server or the role may not install it.

Example ​

ts
await queryInterface.createExtension('pg_trgm', { ifNotExists: true });

See ​

Schema guide

Inherited from ​

QueryInterfaceCatalog.createExtension


createFunction() ​

createFunction(name, params, returnType, language, body, optionsArray?, options?): Promise<void>

Defined in: src/query-interface/query-interface-routines.ts:63

Creates a function with CREATE [OR REPLACE] FUNCTION (Sequelize v6 createFunction). The body is wrapped in BEGIN ... END; as in v6.

The body is SQL text you provide: it is not escaped, so never build it from user input. Names, parameter names and the language are validated identifier words; types and characteristic keywords are checked against strict patterns and allowlists. A function name given as a string is unquoted (folds to lower case, like v6); use { name, schema } for an exact quoted name.

Parameters ​

name ​

FunctionName

Function name ('touch', 'app.touch' or { name, schema }).

params ​

readonly Readonly<{ direction?: string; name?: string; type: string; }>[]

Parameters { type, name?, direction? }; pass [] for none.

returnType ​

string

Return type, e.g. 'trigger', 'integer', 'SETOF text'.

language ​

string

Language, e.g. 'plpgsql'.

body ​

string

Trusted function body without BEGIN / END.

optionsArray? ​

readonly string[] = []

Characteristic keywords such as 'IMMUTABLE', 'SECURITY DEFINER', 'COST 10'.

options? ​

Readonly<{ force?: boolean; variables?: readonly Readonly<{ default?: unknown; name: string; type: string; }>[]; }> & QueryOptionsBase = {}

variables, force (OR REPLACE) and query options.

Returns ​

Promise<void>

Throws ​

Error when a part is missing or fails validation.

Throws ​

DatabaseError when PostgreSQL rejects the function.

Example ​

ts
await queryInterface.createFunction(
  'touch_updated_at',
  [],
  'trigger',
  'plpgsql',
  'NEW."updatedAt" := now(); RETURN NEW;',
  ['VOLATILE'],
  { force: true },
);

See ​

Schema objects guide

Inherited from ​

QueryInterfaceCatalog.createFunction


createMaterializedView() ​

createMaterializedView(name, query, options?): Promise<void>

Defined in: src/query-interface/query-interface-views.ts:84

postgrelize extension

Creates a materialized view with CREATE MATERIALIZED VIEW.

The query is trusted SQL text, as in QueryInterface.createView. PostgreSQL has no OR REPLACE for materialized views: drop and create, or use ifNotExists.

Parameters ​

name ​

TableName

View name or { tableName, schema }.

query ​

string

Trusted SELECT text.

options? ​

Readonly<{ columns?: readonly string[]; ifNotExists?: boolean; using?: string; withData?: boolean; }> & QueryOptionsBase = {}

ifNotExists, columns, using, withData and query options.

Returns ​

Promise<void>

Throws ​

Error for an empty query or an invalid access method.

Example ​

ts
await queryInterface.createMaterializedView('post_stats', 'SELECT author_id, count(*) AS n FROM "Posts" GROUP BY author_id');

Inherited from ​

QueryInterfaceCatalog.createMaterializedView


createPartition() ​

createPartition(parent, partition, bound, options?): Promise<void>

Defined in: src/query-interface/query-interface-partitions.ts:44

postgrelize extension

Creates a partition of a partitioned table (CREATE TABLE ... PARTITION OF). The partitioned parent comes from createTable with partitionBy.

Parameters ​

parent ​

TableName

Partitioned table name or { tableName, schema }.

partition ​

TableName

New partition name or { tableName, schema }.

bound ​

PartitionBoundSpec

{ in } (list), { from, to } (range), { modulus, remainder } (hash) or { default: true }; values are rendered as literals.

options? ​

Readonly<{ ifNotExists?: boolean; partitionBy?: Readonly<{ columns: readonly PartitionKeyElement[]; strategy: PartitionStrategy; }>; }> & QueryOptionsBase = {}

partitionBy (sub-partitioning), ifNotExists and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.createTable('events', { at: DataTypes.DATE }, {
  partitionBy: { strategy: 'range', columns: ['at'] },
});
await queryInterface.createPartition('events', 'events_2026', {
  from: ['2026-01-01'],
  to: ['2027-01-01'],
});

See ​

Constraints and indexes guide

Inherited from ​

QueryInterfaceCatalog.createPartition


createPolicy() ​

createPolicy(table, name, options?): Promise<void>

Defined in: src/query-interface/query-interface-policies.ts:87

postgrelize extension

Creates a row-level security policy with CREATE POLICY.

using and withCheck are SQL text you provide (not escaped, never build them from user input) or a where object, whose values are rendered as escaped literals. Session variables set with the settings transaction option work in conditions: "tenant_id = current_setting('app.tenant_id')::int".

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

name ​

string

Policy name.

options? ​

Readonly<{ command?: "all" | "update" | "delete" | "insert" | "select"; kind?: "permissive" | "restrictive"; roles?: readonly string[]; using?: PolicyCondition; withCheck?: PolicyCondition; }> & QueryOptionsBase = {}

command, kind, roles, using, withCheck and query options.

Returns ​

Promise<void>

Throws ​

Error for an unknown command or kind, empty roles or an empty condition.

Example ​

ts
await queryInterface.createPolicy('documents', 'tenant_isolation', {
  roles: ['app_user'],
  using: "tenant_id = current_setting('app.tenant_id')::int",
  withCheck: "tenant_id = current_setting('app.tenant_id')::int",
});

Inherited from ​

QueryInterfaceCatalog.createPolicy


createSchema() ​

createSchema(schema, options?): Promise<void>

Defined in: src/query-interface/query-interface-tables.ts:44

Runs CREATE SCHEMA IF NOT EXISTS for a schema.

Parameters ​

schema ​

string

Schema name.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.createSchema('tenant_1');

Inherited from ​

QueryInterfaceCatalog.createSchema


createSequence() ​

createSequence(name, options?): Promise<void>

Defined in: src/query-interface/query-interface-objects.ts:82

postgrelize extension

Creates a sequence with CREATE SEQUENCE. Numbers must be whole (number or bigint).

Parameters ​

name ​

TableName

Sequence name or { tableName, schema }.

options? ​

Readonly<{ cache?: SequenceNumber; cycle?: boolean; dataType?: "bigint" | "integer" | "smallint"; ifNotExists?: boolean; increment?: SequenceNumber; maxValue?: false | SequenceNumber; minValue?: false | SequenceNumber; ownedBy?: Readonly<{ column: string; table: TableName; }> | null; start?: SequenceNumber; }> & QueryOptionsBase = {}

Sequence properties and query options.

Returns ​

Promise<void>

Throws ​

Error for an unknown dataType or a non-integer number.

Example ​

ts
await queryInterface.createSequence('invoice_number', { start: 1000, increment: 1, cache: 10 });

Inherited from ​

QueryInterfaceCatalog.createSequence


createTable() ​

createTable(table, attributes, options?): Promise<void>

Defined in: src/query-interface/query-interface-tables.ts:112

Creates a table together with the enum types its columns need, if the table does not exist yet. Foreign keys, defaults and comments come from the attribute definitions.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

attributes ​

ModelAttributes

Column definitions keyed by column name.

options? ​

CreateTableOptions = {}

Comment and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.createTable(
  { tableName: 'posts', schema: 'tenant_1' },
  {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
    title: { type: DataTypes.STRING, allowNull: false },
  },
  { comment: 'Blog posts' },
);

Inherited from ​

QueryInterfaceCatalog.createTable


createTrigger() ​

createTrigger(table, name, timing, events, functionName, functionArgs?, optionsArray?, options?): Promise<void>

Defined in: src/query-interface/query-interface-routines.ts:145

Creates a trigger with CREATE TRIGGER (Sequelize v6 createTrigger; 'after_constraint' creates a CONSTRAINT TRIGGER).

functionArgs are rendered as literals (v6 rendered typed parameter declarations, which PostgreSQL rejects). The optionsArray clauses are checked against an allowlist ('FOR EACH ROW', 'FOR EACH STATEMENT', 'DEFERRABLE', 'INITIALLY DEFERRED', REFERENCING ... TABLE AS name); a WHEN (condition) clause is trusted SQL and is not escaped.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

name ​

string

Trigger name.

timing ​

TriggerTiming

'before', 'after', 'instead_of' or 'after_constraint'.

events ​

readonly TriggerEvent[]

Events that fire the trigger: 'insert', 'update', 'delete', 'truncate'.

functionName ​

FunctionName

Trigger function (see QueryInterface.createFunction).

functionArgs? ​

readonly TriggerArgument[] = []

Literal arguments for the function; pass [] for none.

optionsArray? ​

readonly string[] = []

Clauses such as ['FOR EACH ROW'].

options? ​

Readonly<{ replace?: boolean; }> & QueryOptionsBase = {}

replace (OR REPLACE, PostgreSQL 14+) and query options.

Returns ​

Promise<void>

Throws ​

Error for an unknown timing, event or clause.

Example ​

ts
await queryInterface.createTrigger('Posts', 'posts_touch', 'before', ['insert', 'update'], 'touch_updated_at', [], ['FOR EACH ROW']);

Inherited from ​

QueryInterfaceCatalog.createTrigger


createView() ​

createView(name, query, options?): Promise<void>

Defined in: src/query-interface/query-interface-views.ts:44

postgrelize extension

Creates a view with CREATE VIEW; replace renders CREATE OR REPLACE VIEW.

The query is SQL text you provide: it is not escaped, so never build it from user input. Names and column lists are quoted identifiers. A trailing ; is removed.

Parameters ​

name ​

TableName

View name or { tableName, schema }.

query ​

string

Trusted SELECT text.

options? ​

Readonly<{ checkOption?: "local" | "cascaded"; columns?: readonly string[]; recursive?: boolean; replace?: boolean; securityBarrier?: boolean; securityInvoker?: boolean; temporary?: boolean; }> & QueryOptionsBase = {}

replace, temporary, recursive, columns, checkOption, securityBarrier, securityInvoker and query options.

Returns ​

Promise<void>

Throws ​

Error for an empty query, a recursive view without columns or an unknown checkOption.

Example ​

ts
await queryInterface.createView('published_posts', 'SELECT id, title FROM "Posts" WHERE published', { replace: true });

See ​

Schema objects guide

Inherited from ​

QueryInterfaceCatalog.createView


databaseVersion() ​

databaseVersion(options?): Promise<string>

Defined in: src/query-interface/query-interface-catalog.ts:217

Reads the PostgreSQL server version (SHOW server_version), e.g. '17.2'.

Parameters ​

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<string>

Version string.

Example ​

ts
const version = await queryInterface.databaseVersion();

Inherited from ​

QueryInterfaceCatalog.databaseVersion


deferConstraints() ​

deferConstraints(transaction, options?): Promise<void>

Defined in: src/query-interface/query-interface-catalog.ts:269

Runs SET CONSTRAINTS ... for options.deferrable (Deferrable.SET_DEFERRED / SET_IMMEDIATE) on the connection of a transaction.

Parameters ​

transaction ​

Transaction

Transaction to run in.

options? ​

QueryOptionsBase & Readonly<{ deferrable?: Readonly<{ sql: string; }>; }> = {}

deferrable and query options.

Returns ​

Promise<void>

Throws ​

Error when deferrable is not Deferrable.SET_DEFERRED / SET_IMMEDIATE.

Example ​

ts
await queryInterface.deferConstraints(transaction, { deferrable: Deferrable.SET_DEFERRED(['posts_user_fk']) });

Inherited from ​

QueryInterfaceCatalog.deferConstraints


describeTable() ​

describeTable(table, options?): Promise<Record<string, Readonly<{ allowNull: boolean; comment: string | null; defaultValue: unknown; primaryKey: boolean; special: readonly string[]; type: string; }>>>

Defined in: src/query-interface/query-interface-tables.ts:218

Reads the column metadata of a table from the catalog.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

options? ​

QueryOptionsBase & Readonly<{ schema?: string; }> = {}

schema and query options.

Returns ​

Promise<Record<string, Readonly<{ allowNull: boolean; comment: string | null; defaultValue: unknown; primaryKey: boolean; special: readonly string[]; type: string; }>>>

Column descriptions keyed by column name.

Throws ​

Error when the table does not exist.

Example ​

ts
const columns = await queryInterface.describeTable('users');
const emailNullable = columns['email']?.allowNull;

Inherited from ​

QueryInterfaceCatalog.describeTable


detachPartition() ​

detachPartition(parent, partition, options?): Promise<void>

Defined in: src/query-interface/query-interface-partitions.ts:92

postgrelize extension

Detaches a partition, keeping it as a standalone table (ALTER TABLE ... DETACH PARTITION).

Parameters ​

parent ​

TableName

Partitioned table name or { tableName, schema }.

partition ​

TableName

Partition name or { tableName, schema }.

options? ​

Readonly<{ concurrently?: boolean; finalize?: boolean; }> & QueryOptionsBase = {}

concurrently (PostgreSQL 14+, not inside a transaction), finalize and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.detachPartition('events', 'events_2026', { concurrently: true });

Inherited from ​

QueryInterfaceCatalog.detachPartition


disableRowLevelSecurity() ​

disableRowLevelSecurity(table, options?): Promise<void>

Defined in: src/query-interface/query-interface-policies.ts:60

postgrelize extension

Turns off row-level security for a table (ALTER TABLE ... DISABLE ROW LEVEL SECURITY); policies are kept.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

options? ​

Readonly<{ noForce?: boolean; }> & QueryOptionsBase = {}

noForce and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.disableRowLevelSecurity('documents', { noForce: true });

Inherited from ​

QueryInterfaceCatalog.disableRowLevelSecurity


dropAllEnums() ​

dropAllEnums(options?): Promise<void>

Defined in: src/query-interface/query-interface.ts:298

Drops every enum type of the current schema with CASCADE, which also drops dependent columns' defaults. Destructive.

Parameters ​

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropAllEnums();

dropAllSchemas() ​

dropAllSchemas(options?): Promise<void>

Defined in: src/query-interface/query-interface-tables.ts:88

Drops, with CASCADE, every schema listed by QueryInterface.showAllSchemas Destructive.

Parameters ​

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropAllSchemas();

Inherited from ​

QueryInterfaceCatalog.dropAllSchemas


dropAllTables() ​

dropAllTables(options?): Promise<void>

Defined in: src/query-interface/query-interface-tables.ts:145

Drops every table of the current schema with CASCADE, except the skipped ones. Destructive.

Parameters ​

options? ​

QueryOptionsBase & Readonly<{ skip?: readonly string[]; }> = {}

skip: table names to keep, plus query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropAllTables({ skip: ['SequelizeMeta'] });

Inherited from ​

QueryInterfaceCatalog.dropAllTables


dropDatabase() ​

dropDatabase(database, options?): Promise<void>

Defined in: src/query-interface/query-interface-catalog.ts:253

Runs DROP DATABASE IF EXISTS. Must run outside a transaction.

Parameters ​

database ​

string

Database name (quoted).

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropDatabase('tenant_42');

Inherited from ​

QueryInterfaceCatalog.dropDatabase


dropEnum() ​

dropEnum(name, options?): Promise<void>

Defined in: src/query-interface/query-interface.ts:284

Runs DROP TYPE IF EXISTS for an enum type.

Parameters ​

name ​

string

Enum type name.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropEnum('enum_users_role');

dropExtension() ​

dropExtension(name, options?): Promise<void>

Defined in: src/query-interface/query-interface-objects.ts:62

postgrelize extension

Removes an extension with DROP EXTENSION.

Parameters ​

name ​

string

Extension name.

options? ​

Readonly<{ cascade?: boolean; ifExists?: boolean; }> & QueryOptionsBase = {}

ifExists, cascade and query options.

Returns ​

Promise<void>

Throws ​

DatabaseError when objects depend on the extension and cascade is not set.

Example ​

ts
await queryInterface.dropExtension('pg_trgm', { ifExists: true });

Inherited from ​

QueryInterfaceCatalog.dropExtension


dropFunction() ​

dropFunction(name, params, options?): Promise<void>

Defined in: src/query-interface/query-interface-routines.ts:95

Drops a function with DROP FUNCTION ... RESTRICT (Sequelize v6 dropFunction).

Parameters ​

name ​

FunctionName

Function name.

params ​

readonly Readonly<{ direction?: string; name?: string; type: string; }>[]

Parameters that identify the overload; pass [] for none.

options? ​

Readonly<{ cascade?: boolean; ifExists?: boolean; }> & QueryOptionsBase = {}

ifExists, cascade and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropFunction('touch_updated_at', [], { ifExists: true });

Inherited from ​

QueryInterfaceCatalog.dropFunction


dropMaterializedView() ​

dropMaterializedView(name, options?): Promise<void>

Defined in: src/query-interface/query-interface-views.ts:124

postgrelize extension

Drops a materialized view with DROP MATERIALIZED VIEW.

Parameters ​

name ​

TableName

View name or { tableName, schema }.

options? ​

Readonly<{ cascade?: boolean; ifExists?: boolean; }> & QueryOptionsBase = {}

ifExists, cascade and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropMaterializedView('post_stats', { ifExists: true });

Inherited from ​

QueryInterfaceCatalog.dropMaterializedView


dropPolicy() ​

dropPolicy(table, name, options?): Promise<void>

Defined in: src/query-interface/query-interface-policies.ts:130

postgrelize extension

Drops a policy with DROP POLICY.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

name ​

string

Policy name.

options? ​

Readonly<{ cascade?: boolean; ifExists?: boolean; }> & QueryOptionsBase = {}

ifExists, cascade and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropPolicy('documents', 'tenant_isolation', { ifExists: true });

Inherited from ​

QueryInterfaceCatalog.dropPolicy


dropSchema() ​

dropSchema(schema, options?): Promise<void>

Defined in: src/query-interface/query-interface-tables.ts:59

Runs DROP SCHEMA IF EXISTS ... CASCADE, removing the schema and everything in it.

Parameters ​

schema ​

string

Schema name.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropSchema('tenant_1');

Inherited from ​

QueryInterfaceCatalog.dropSchema


dropSequence() ​

dropSequence(name, options?): Promise<void>

Defined in: src/query-interface/query-interface-objects.ts:121

postgrelize extension

Drops a sequence with DROP SEQUENCE.

Parameters ​

name ​

TableName

Sequence name or { tableName, schema }.

options? ​

Readonly<{ cascade?: boolean; ifExists?: boolean; }> & QueryOptionsBase = {}

ifExists, cascade and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropSequence('invoice_number', { ifExists: true });

Inherited from ​

QueryInterfaceCatalog.dropSequence


dropTable() ​

dropTable(table, options?): Promise<void>

Defined in: src/query-interface/query-interface-tables.ts:131

Runs DROP TABLE IF EXISTS for a table; it does not drop the table's enum types.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

options? ​

DropTableOptions = {}

cascade and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropTable('posts', { cascade: true });

Inherited from ​

QueryInterfaceCatalog.dropTable


dropTrigger() ​

dropTrigger(table, name, options?): Promise<void>

Defined in: src/query-interface/query-interface-routines.ts:171

Drops a trigger with DROP TRIGGER ... RESTRICT (Sequelize v6 dropTrigger).

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

name ​

string

Trigger name.

options? ​

Readonly<{ cascade?: boolean; ifExists?: boolean; }> & QueryOptionsBase = {}

ifExists, cascade and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropTrigger('Posts', 'posts_touch', { ifExists: true });

Inherited from ​

QueryInterfaceCatalog.dropTrigger


dropView() ​

dropView(name, options?): Promise<void>

Defined in: src/query-interface/query-interface-views.ts:64

postgrelize extension

Drops a view with DROP VIEW.

Parameters ​

name ​

TableName

View name or { tableName, schema }.

options? ​

Readonly<{ cascade?: boolean; ifExists?: boolean; }> & QueryOptionsBase = {}

ifExists, cascade and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.dropView('published_posts', { ifExists: true });

Inherited from ​

QueryInterfaceCatalog.dropView


enableRowLevelSecurity() ​

enableRowLevelSecurity(table, options?): Promise<void>

Defined in: src/query-interface/query-interface-policies.ts:41

postgrelize extension

Turns on row-level security for a table (ALTER TABLE ... ENABLE ROW LEVEL SECURITY). Without policies, non-owners see no rows.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

options? ​

Readonly<{ force?: boolean; }> & QueryOptionsBase = {}

force (also applies to the table owner) and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.enableRowLevelSecurity('documents', { force: true });

See ​

Schema objects guide

Inherited from ​

QueryInterfaceCatalog.enableRowLevelSecurity


getForeignKeyReferencesForTable() ​

getForeignKeyReferencesForTable(table, options?): Promise<Readonly<{ columnName: string; constraintName: string; referencedColumnName: string; referencedTableName: string; }>[]>

Defined in: src/query-interface/query-interface-tables.ts:238

Lists the foreign key columns of a table with the table and column each one references.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<Readonly<{ columnName: string; constraintName: string; referencedColumnName: string; referencedTableName: string; }>[]>

Constraint name, column and referenced table/column per foreign key column.

Example ​

ts
const references = await queryInterface.getForeignKeyReferencesForTable('posts');
const targets = references.map((reference) => reference.referencedTableName);

Inherited from ​

QueryInterfaceCatalog.getForeignKeyReferencesForTable


getForeignKeysForTables() ​

getForeignKeysForTables(tables, options?): Promise<Record<string, readonly string[]>>

Defined in: src/query-interface/query-interface-catalog.ts:320

Names of the foreign key constraints of each table.

Parameters ​

tables ​

readonly TableName[]

Table names or { tableName, schema }.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<Record<string, readonly string[]>>

Constraint names keyed by table (schema.table for qualified tables).

Example ​

ts
const keys = await queryInterface.getForeignKeysForTables(['posts', 'comments']);

Inherited from ​

QueryInterfaceCatalog.getForeignKeysForTables


nextval() ​

nextval(name, options?): Promise<string>

Defined in: src/query-interface/query-interface-objects.ts:141

postgrelize extension

Advances a sequence and returns the new value (SELECT nextval(...); the name is bound).

Parameters ​

name ​

TableName

Sequence name or { tableName, schema }.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<string>

The value as text, so bigint values stay exact.

Example ​

ts
const next = await queryInterface.nextval('invoice_number');

Inherited from ​

QueryInterfaceCatalog.nextval


quoteIdentifier() ​

quoteIdentifier(identifier, _force?): string

Defined in: src/query-interface/query-interface-catalog.ts:186

Quotes an identifier for SQL: "name", embedded double quotes are doubled.

Parameters ​

identifier ​

string

Identifier.

_force? ​

boolean

Accepted for Sequelize v6 compatibility; identifiers are always quoted.

Returns ​

string

Quoted identifier.

Example ​

ts
queryInterface.quoteIdentifier('user name'); // "user name"

Inherited from ​

QueryInterfaceCatalog.quoteIdentifier


quoteIdentifiers() ​

quoteIdentifiers(identifiers): string

Defined in: src/query-interface/query-interface-catalog.ts:200

Quotes a dotted identifier part by part: schema.table becomes "schema"."table".

Parameters ​

identifiers ​

string

Dot separated identifiers.

Returns ​

string

Quoted identifiers.

Example ​

ts
queryInterface.quoteIdentifiers('tenant.users'); // "tenant"."users"

Inherited from ​

QueryInterfaceCatalog.quoteIdentifiers


refreshMaterializedView() ​

refreshMaterializedView(name, options?): Promise<void>

Defined in: src/query-interface/query-interface-views.ts:105

postgrelize extension

Recomputes a materialized view with REFRESH MATERIALIZED VIEW.

Parameters ​

name ​

TableName

View name or { tableName, schema }.

options? ​

Readonly<{ concurrently?: boolean; withData?: boolean; }> & QueryOptionsBase = {}

concurrently (needs a unique index), withData and query options.

Returns ​

Promise<void>

Throws ​

Error when concurrently is combined with withData: false.

Example ​

ts
await queryInterface.refreshMaterializedView('post_stats', { concurrently: true });

Inherited from ​

QueryInterfaceCatalog.refreshMaterializedView


removeColumn() ​

removeColumn(table, column, options?): Promise<void>

Defined in: src/query-interface/query-interface.ts:100

Drops a column with ALTER TABLE ... DROP COLUMN.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

column ​

string

Column name.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.removeColumn('users', 'nickname');

removeConstraint() ​

removeConstraint(table, name, options?): Promise<void>

Defined in: src/query-interface/query-interface.ts:265

Drops a named constraint from a table.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

name ​

string

Constraint name.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.removeConstraint('products', 'products_price_positive');

removeIndex() ​

removeIndex(table, nameOrFields, options?): Promise<void>

Defined in: src/query-interface/query-interface.ts:184

Drops an index identified by name, or by the fields it was created on (the default generated name is derived from them).

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

nameOrFields ​

string | readonly IndexField[]

Index name or indexed fields.

options? ​

QueryOptionsBase & Readonly<{ concurrently?: boolean; }> = {}

concurrently and query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.removeIndex('users', 'users_email_unique');

renameColumn() ​

renameColumn(table, before, after, options?): Promise<void>

Defined in: src/query-interface/query-interface.ts:139

Renames a column with ALTER TABLE ... RENAME COLUMN.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

before ​

string

Current column name.

after ​

string

New column name.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.renameColumn('users', 'name', 'full_name');

renameFunction() ​

renameFunction(name, params, newName, options?): Promise<void>

Defined in: src/query-interface/query-interface-routines.ts:116

Renames a function with ALTER FUNCTION ... RENAME TO (Sequelize v6 renameFunction).

Parameters ​

name ​

FunctionName

Current function name.

params ​

readonly Readonly<{ direction?: string; name?: string; type: string; }>[]

Parameters that identify the overload.

newName ​

string

New unqualified name.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.renameFunction('touch_updated_at', [], 'set_updated_at');

Inherited from ​

QueryInterfaceCatalog.renameFunction


renameTable() ​

renameTable(before, after, options?): Promise<void>

Defined in: src/query-interface/query-interface-tables.ts:163

Renames a table with ALTER TABLE ... RENAME TO; the schema of after is ignored.

Parameters ​

before ​

TableName

Current table.

after ​

TableName

New table name.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.renameTable('posts', 'articles');

Inherited from ​

QueryInterfaceCatalog.renameTable


renameTrigger() ​

renameTrigger(table, name, newName, options?): Promise<void>

Defined in: src/query-interface/query-interface-routines.ts:192

Renames a trigger with ALTER TRIGGER ... RENAME TO (Sequelize v6 renameTrigger).

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

name ​

string

Current trigger name.

newName ​

string

New trigger name.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.renameTrigger('Posts', 'posts_touch', 'posts_set_updated_at');

Inherited from ​

QueryInterfaceCatalog.renameTrigger


setval() ​

setval(name, value, isCalled?, options?): Promise<string>

Defined in: src/query-interface/query-interface-objects.ts:160

postgrelize extension

Sets the current value of a sequence (SELECT setval(...); name and value are bound).

Parameters ​

name ​

TableName

Sequence name or { tableName, schema }.

value ​

SequenceNumber

New value (number or bigint).

isCalled? ​

boolean = true

true (default): the next nextval returns value + increment; false: it returns value.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<string>

The value set, as text.

Example ​

ts
await queryInterface.setval('invoice_number', 9000, false);

Inherited from ​

QueryInterfaceCatalog.setval


showAllSchemas() ​

showAllSchemas(options?): Promise<string[]>

Defined in: src/query-interface/query-interface-tables.ts:74

Lists application schemas, leaving out public, information_schema and pg_* schemas.

Parameters ​

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<string[]>

Schema names.

Example ​

ts
const schemas = await queryInterface.showAllSchemas();

Inherited from ​

QueryInterfaceCatalog.showAllSchemas


showAllTables() ​

showAllTables(options?): Promise<string[]>

Defined in: src/query-interface/query-interface-tables.ts:200

Lists the table names of the current schema in alphabetical order.

Parameters ​

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<string[]>

Table names.

Example ​

ts
const tables = await queryInterface.showAllTables(); // e.g. ['posts', 'users']

Inherited from ​

QueryInterfaceCatalog.showAllTables


showConstraint() ​

showConstraint(table, constraintName?, options?): Promise<Readonly<Record<string, unknown>>[]>

Defined in: src/query-interface/query-interface-catalog.ts:162

Lists the constraints of a table (information_schema.table_constraints), optionally one by name.

Parameters ​

table ​

TableName

Table name or { tableName, schema }; current_schema() when no schema is given.

constraintName? ​

string | null

Only this constraint. Sequelize v6 ignores it for PostgreSQL and lists all.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<Readonly<Record<string, unknown>>[]>

Constraint rows (constraintName, constraintType, isDeferrable, ...), ordered by name.

Example ​

ts
const constraints = await queryInterface.showConstraint('users');
const names = constraints.map((constraint) => constraint.constraintName);

Inherited from ​

QueryInterfaceCatalog.showConstraint


showIndex() ​

showIndex(table, options?): Promise<Readonly<{ definition: string; fields: readonly Readonly<{ attribute: string; collate: string | undefined; length: undefined; order: "ASC" | "DESC" | undefined; }>[]; indkey: string; name: string; primary: boolean; unique: boolean; }>[]>

Defined in: src/query-interface/query-interface-catalog.ts:140

Lists the indexes of a table, with their columns, uniqueness and definition.

Parameters ​

table ​

TableName

Table name or { tableName, schema }; current_schema() when no schema is given.

options? ​

QueryOptionsBase = {}

Query options.

Returns ​

Promise<Readonly<{ definition: string; fields: readonly Readonly<{ attribute: string; collate: string | undefined; length: undefined; order: "ASC" | "DESC" | undefined; }>[]; indkey: string; name: string; primary: boolean; unique: boolean; }>[]>

One entry per index, ordered by name.

Example ​

ts
const indexes = await queryInterface.showIndex('users');
const unique = indexes.filter((index) => index.unique).map((index) => index.name);

Inherited from ​

QueryInterfaceCatalog.showIndex


tableExists() ​

tableExists(table, options?): Promise<boolean>

Defined in: src/query-interface/query-interface-tables.ts:185

Tells whether a table exists in its schema (the current schema when none is given).

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<boolean>

True when the table exists.

Example ​

ts
if (!(await queryInterface.tableExists('posts'))) {
  await queryInterface.createTable('posts', { title: DataTypes.STRING });
}

Inherited from ​

QueryInterfaceCatalog.tableExists


validateConstraint() ​

validateConstraint(table, name, options?): Promise<void>

Defined in: src/query-interface/query-interface.ts:245

postgrelize extension

Validates a constraint added with notValid: true, scanning existing rows without blocking writes.

Parameters ​

table ​

TableName

Table name or { tableName, schema }.

name ​

string

Constraint name.

options? ​

QueryOptionsBase

Query options.

Returns ​

Promise<void>

Example ​

ts
await queryInterface.addConstraint('products', {
  type: 'check',
  name: 'products_price_positive',
  where: { price: { [Op.gt]: 0 } },
  notValid: true,
});
await queryInterface.validateConstraint('products', 'products_price_positive');

Released under the MIT License.