Back to Sequelize

Mixin BelongsToMany

static/v3/api/associations/belongs-to-many/index.html

latest6.9 KB
Original Source

Mixin BelongsToMany

View code

Many-to-many association with a join table.

When the join table has additional attributes, these can be passed in the options object:

UserProject = sequelize.define('user_project', {
  role: Sequelize.STRING
});
User.belongsToMany(Project, { through: UserProject });
Project.belongsToMany(User, { through: UserProject });
// through is required!

user.addProject(project, { role: 'manager', transaction: t });

All methods allow you to pass either a persisted instance, its primary key, or a mixture:

Project.create({ id: 11 }).then(function (project) {
  user.addProjects([project, 12]);
});

In the API reference below, replace Assocation(s) with the actual name of your association, e.g. for User.belongsToMany(Project) the getter will be user.getProjects().


getAssociations([options]) -> Promise.<Array.<Instance>>

View code

Get everything currently associated with this, using an optional where clause.

Params:

NameTypeDescription
[options]Object
[options.where]ObjectAn optional where clause to limit the associated models
[options.scope]StringBoolean
[options.schema]StringApply a schema on the related model

setAssociations([newAssociations], [options]) -> Promise

View code

Set the associated models by passing an array of instances or their primary keys. Everything that it not in the passed array will be un-associated.

Params:

NameTypeDescription
[newAssociations]Array.<InstanceString
[options]ObjectOptions passed to through.findAll, bulkCreate, update and destroy. Can also hold additional attributes for the join table
[options.validate]ObjectRun validation for the join model

addAssociations([newAssociations], [options]) -> Promise

View code

Associate several instances with this.

Params:

NameTypeDescription
[newAssociations]Array.<InstanceString
[options]ObjectOptions passed to through.findAll, bulkCreate and update. Can also hold additional attributes for the join table.
[options.validate]ObjectRun validation for the join model.

addAssociation([newAssociation], [options]) -> Promise

View code

Associate one instance with this.

Params:

NameTypeDescription
[newAssociation]InstanceString
[options]ObjectOptions passed to through.findAll, bulkCreate and update. Can also hold additional attributes for the join table.
[options.validate]ObjectRun validation for the join model.

createAssociation([values], [options]) -> Promise

View code

Create a new instance of the associated model and associate it with this.

Params:

NameTypeDescription
[values]Object
[options]ObjectOptions passed to create and add. Can also hold additional attributes for the join table

removeAssociation([oldAssociated], [options]) -> Promise

View code

Un-associate the instance.

Params:

NameTypeDescription
[oldAssociated]InstanceString
[options]ObjectOptions passed to through.destroy

removeAssociations([oldAssociated], [options]) -> Promise

View code

Un-associate several instances.

Params:

NameTypeDescription
[oldAssociated]Array.<InstanceString
[options]ObjectOptions passed to through.destroy

hasAssociation([instance], [options]) -> Promise

View code

Check if an instance is associated with this.

Params:

NameTypeDescription
[instance]InstanceString
[options]ObjectOptions passed to getAssociations

hasAssociations([instances], [options]) -> Promise

View code

Check if all instances are associated with this.

Params:

NameTypeDescription
[instances]Array.<InstanceString
[options]ObjectOptions passed to getAssociations

countAssociations([options]) -> Promise.<Int>

View code

Count everything currently associated with this, using an optional where clause.

Params:

NameTypeDescription
[options]Object
[options.where]ObjectAn optional where clause to limit the associated models
[options.scope]StringBoolean

This document is automatically generated based on source code comments. Please do not edit it directly, as your changes will be ignored. Please write on IRC, open an issue or a create a pull request if you feel something can be improved. For help on how to write source code documentation see JSDoc and dox