1. SELECT * FROM projects
    2. INNER JOIN users ON projects.userId = users.id
    3. WHERE projects.deleted = true
    4. AND users.active = true

    If you want to apply another scope alongside the default scope, pass the key defaultScope to .scope:

    1. Project.scope('defaultScope', 'deleted').findAll();

    When invoking several scopes, keys from subsequent scopes will overwrite previous ones (similarly to ), except for where and include, which will be merged. Consider two scopes:

    1. {
    2. scope1: {
    3. where: {
    4. firstName: 'bob',
    5. age: {
    6. [Op.gt]: 20
    7. }
    8. },
    9. limit: 2
    10. },
    11. where: {
    12. age: {
    13. [Op.gt]: 30
    14. },
    15. limit: 10
    16. }
    17. }

    Calling .scope('scope1', 'scope2') will yield the following query

    1. WHERE firstName = 'bob' AND age > 30 LIMIT 10

    Note how limit and age are overwritten by scope2, while firstName is preserved. The limit, offset, order, paranoid, lock and raw fields are overwritten, while where is shallowly merged (meaning that identical keys will be overwritten). The merge strategy for include will be discussed later on.

    The same merge logic applies when passing a find object directly to findAll (and similar finders) on a scoped model:

    1. WHERE deleted = true AND firstName = 'john'

    Here the deleted scope is merged with the finder. If we were to pass where: { firstName: 'john', deleted: false } to the finder, the deleted scope would be overwritten.

    Includes are merged recursively based on the models being included. This is a very powerful merge, added on v5, and is better understood with an example.

    Consider four models: Foo, Bar, Baz and Qux, with has-many associations as follows:

    1. class Foo extends Model {}
    2. class Bar extends Model {}
    3. class Baz extends Model {}
    4. class Qux extends Model {}
    5. Foo.init({ name: Sequelize.STRING }, { sequelize });
    6. Qux.init({ name: Sequelize.STRING }, { sequelize });
    7. Foo.hasMany(Bar, { foreignKey: 'fooId' });
    8. Bar.hasMany(Baz, { foreignKey: 'barId' });
    9. Baz.hasMany(Qux, { foreignKey: 'bazId' });

    These four scopes can be deeply merged easily, for example by calling Foo.scope('includeEverything', 'limitedBars', 'limitedBazs', 'excludeBazName').findAll(), which would be entirely equivalent to calling the following:

    1. Foo.findAll({
    2. include: {
    3. model: this.Bar,
    4. limit: 2,
    5. include: [{
    6. model: this.Baz,
    7. limit: 2,
    8. attributes: {
    9. exclude: ['name']
    10. },
    11. include: this.Qux
    12. }]
    13. });

    Observe how the four scopes were merged into one. The includes of scopes are merged based on the model being included. If one scope includes model A and another includes model B, the merged result will include both models A and B. On the other hand, if both scopes include the same model A, but with different options (such as nested includes or other attributes), those will be merged recursively, as shown above.

    The merge illustrated above works in the exact same way regardless of the order applied to the scopes. The order would only make a difference if a certain option was set by two different scopes - which is not the case of the above example, since each scope does a different thing.

    This merge strategy also works in the exact same way with options passed to .findAll, .findOne and the like.