This is a temporary class used to progressively migrate the AbstractQueryInterface class to TypeScript by slowly moving its functions here. Always use AbstractQueryInterface instead.

Type Parameters

Hierarchy (view full)

Constructors

Properties

#internalQueryInterface: AbstractQueryInterfaceInternal
dialect: Dialect

Accessors

Methods

  • Add a constraint to a table

    Available constraints:

    • UNIQUE
    • DEFAULT (MSSQL only)
    • CHECK (Not supported by MySQL)
    • FOREIGN KEY
    • PRIMARY KEY

    Parameters

    Returns Promise<void>

    Example: UNIQUE

    queryInterface.addConstraint('Users', {
    fields: ['email'],
    type: 'UNIQUE',
    name: 'custom_unique_constraint_name'
    });

    Example: CHECK

    queryInterface.addConstraint('Users', {
    fields: ['roles'],
    type: 'CHECK',
    where: {
    roles: ['user', 'admin', 'moderator', 'guest']
    }
    });

    Example: Default - MSSQL only

    queryInterface.addConstraint('Users', {
    fields: ['roles'],
    type: 'DEFAULT',
    defaultValue: 'guest'
    });

    Example: Primary Key

    queryInterface.addConstraint('Users', {
    fields: ['username'],
    type: 'PRIMARY KEY',
    name: 'custom_primary_constraint_name'
    });

    Example: Composite Primary Key

    queryInterface.addConstraint('Users', {
    fields: ['first_name', 'last_name'],
    type: 'PRIMARY KEY',
    name: 'custom_primary_constraint_name'
    });

    Example: Foreign Key

    queryInterface.addConstraint('Posts', {
    fields: ['username'],
    type: 'FOREIGN KEY',
    name: 'custom_fkey_constraint_name',
    references: { //Required field
    table: 'target_table_name',
    field: 'target_column_name'
    },
    onDelete: 'cascade',
    onUpdate: 'cascade'
    });

    Example: Composite Foreign Key

    queryInterface.addConstraint('TableName', {
    fields: ['source_column_name', 'other_source_column_name'],
    type: 'FOREIGN KEY',
    name: 'custom_fkey_constraint_name',
    references: { //Required field
    table: 'target_table_name',
    fields: ['target_column_name', 'other_target_column_name']
    },
    onDelete: 'cascade',
    onUpdate: 'cascade'
    });