New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scopes attributes are not merged #4856

Closed
lifenautjoe opened this Issue Nov 13, 2015 · 5 comments

Comments

5 participants
@lifenautjoe

lifenautjoe commented Nov 13, 2015

Version : 3.13.0
Dialect : mysql

Given

var User = sequelize.define('User', {
    username: {
        type: DataTypes.STRING,
        allowNull: false
    },
    about: {
        type: DataTypes.STRING
    },
    password: {
        type: DataTypes.STRING,
        allowNull: false,
        set: function (val) {
            // Some encrypting logic
        }
    }
}, {
    scopes: {
        'public': {
            attributes: [
                'id',
                'username',
                'about'
            ]
        },
        'private': {
            attributes: [
                'password'
            ]
        }
    }
});

When querying an instance with multiple scopes, scopes attributes do not get merged.
e.g.

User.scope('public','private').findOneById(1).then(function(user){
    console.log(user.about); // Exists, attribute included from public scope
    console.log(user.password); // Undefined, public and private attributes were not merged
});
@mickhansen

This comment has been minimized.

Contributor

mickhansen commented Nov 13, 2015

@janmeier I consider this to be a bug, i don't believe we should merge into find.attributes but merging attributes from scopes seems like the right thing to do.

@janmeier

This comment has been minimized.

Member

janmeier commented Nov 18, 2015

Yep, agree :)

@sushantdhiman

This comment has been minimized.

Member

sushantdhiman commented Dec 23, 2015

Hi @janmeier , @mickhansen , I was working on a fix and its easy to handle when attributes field is an Array. But look at this case of scope as Object

          lowAccessObj: {
             attributes: {
               include: ['other_value', 'access_level'],
               exclude: ['username']
             },
             where: {
               access_level: {
                 lte: 5
               }
             }
           },
           withNameObj: {
             attributes: {
               include: ['username'],
               exclude: ['access_level']
             },
           }

Now if we try to execute the query Model.scope('lowAccessObj','withNameObj').findOne() what should be the attributes included ?

At current code its easy to do application of one scope on other and thus using the resulting values something like this -

First apply scope : `lowAccessObj`
attributes : ['other_value', 'access_level']
Next apply scope : `withNameObj`
attributes : ['other_value', 'username']   // removed  'access_level'
@mickhansen

This comment has been minimized.

Contributor

mickhansen commented Dec 23, 2015

@sushantdhiman Good question.

mickhansen added a commit that referenced this issue Dec 29, 2015

Merge pull request #5093 from sushantdhiman/fix-4856
Fix #4856 : Attributes from multiple scopes doesn't merge
@thatisuday

This comment has been minimized.

thatisuday commented Mar 1, 2017

@mickhansen Is this included in v3.30.2 because it's not working out for me. #7302

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment