Interface BelongsToManyOptions<SourceKey, TargetKey, ThroughModel>

Options provided when associating models with belongsToMany relationship.

Used by Model.belongsToMany.

interface BelongsToManyOptions<SourceKey, TargetKey, ThroughModel> {
    as?: string | {
        plural: string;
        singular: string;
    };
    foreignKey?: Extract<keyof ThroughModel["_attributes"], string> | ForeignKeyOptions<Extract<keyof ThroughModel["_attributes"], string>>;
    foreignKeyConstraints?: boolean;
    hooks?: boolean;
    inverse?: string | {
        as?: string | {
            plural: string;
            singular: string;
        };
        foreignKeyConstraints?: boolean;
        scope?: AssociationScope;
    };
    otherKey?: Extract<keyof ThroughModel["_attributes"], string> | ForeignKeyOptions<Extract<keyof ThroughModel["_attributes"], string>>;
    scope?: AssociationScope;
    sourceKey?: SourceKey;
    targetKey?: TargetKey;
    through: string | MaybeForwardedModelStatic<ThroughModel> | ThroughOptions<ThroughModel>;
    throughAssociations?: {
        fromSource?: string;
        fromTarget?: string;
        toSource?: string;
        toTarget?: string;
    };
}

Type Parameters

  • SourceKey extends string = string
  • TargetKey extends string = string
  • ThroughModel extends Model = Model

Hierarchy (view full)

Properties

as?: string | {
    plural: string;
    singular: string;
}

The alias of this model, in singular form. See also the name option passed to sequelize.define. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the association, you should provide the same alias when eager loading and when getting associated models. Defaults to the singularized name of target

Type declaration

  • plural: string
  • singular: string
foreignKey?: Extract<keyof ThroughModel["_attributes"], string> | ForeignKeyOptions<Extract<keyof ThroughModel["_attributes"], string>>

The configuration of the foreign key Attribute. See Sequelize#define or Model.init for more information about the syntax.

Using a string is equivalent to passing a ForeignKeyOptions object with the ForeignKeyOptions.name option set.

foreignKeyConstraints?: boolean

Should "ON UPDATE", "ON DELETE" and "REFERENCES" constraints be enabled on the foreign key?

This only affects the foreign key that points to the source model. to control the one that points to the target model, set the "foreignKeyConstraints" option in BelongsToManyOptions.inverse.

hooks?: boolean

If false the applicable hooks will not be called. The default value depends on the context.

Default

true
inverse?: string | {
    as?: string | {
        plural: string;
        singular: string;
    };
    foreignKeyConstraints?: boolean;
    scope?: AssociationScope;
}

The name of the inverse association, or an object for further association setup.

Type declaration

  • Optional as?: string | {
        plural: string;
        singular: string;
    }
  • Optional foreignKeyConstraints?: boolean
  • Optional scope?: AssociationScope
otherKey?: Extract<keyof ThroughModel["_attributes"], string> | ForeignKeyOptions<Extract<keyof ThroughModel["_attributes"], string>>

The name of the foreign key attribute in the through model (representing the target model) or an object representing the type definition for the other column (see Sequelize.define for syntax). When using an object, you can add a name property to set the name of the colum. Defaults to the name of target + primary key of target

A key/value set that will be used for association create and find defaults on the target. (sqlite not supported for N:M)

sourceKey?: SourceKey

The name of the attribute to use as the key for the association in the source table. Defaults to the primary key attribute of the source model

targetKey?: TargetKey

The name of the attribute to use as the key for the association in the target table. Defaults to the primary key attribute of the target model

The name of the table that is used to join source and target in n:m associations. Can also be a Sequelize model if you want to define the junction table yourself and add extra attributes to it.

throughAssociations?: {
    fromSource?: string;
    fromTarget?: string;
    toSource?: string;
    toTarget?: string;
}

Configures the name of the associations that will be defined between the source model and the through model, as well as between the target model and the through model.

Type declaration

  • Optional fromSource?: string

    The name of the HasMany association going from the Source model to the Through model.

    By default, the association will be the name of the BelongsToMany association

    • the name of the inverse BelongsToMany association.
  • Optional fromTarget?: string

    The name of the HasMany association going from the Target model to the Through model.

    By default, the association will be the name of the Inverse BelongsToMany association

    • the name of the BelongsToMany association.
  • Optional toSource?: string

    The name of the BelongsTo association going from the Through model to the Source model.

    By default, the association name will be the name of the inverse BelongsToMany association, singularized.

  • Optional toTarget?: string

    The name of the BelongsTo association going from the Through model to the Target model.

    By default, the association name will be the name of the parent BelongsToMany association, singularized.