Skip to content
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 · 6 comments
Closed

Scopes attributes are not merged #4856

lifenautjoe opened this issue Nov 13, 2015 · 6 comments
Assignees

Comments

@lifenautjoe
Copy link

@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
Copy link
Contributor

@mickhansen 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
Copy link
Member

@janmeier janmeier commented Nov 18, 2015

Yep, agree :)

@sushantdhiman
Copy link
Contributor

@sushantdhiman 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
Copy link
Contributor

@mickhansen mickhansen commented Dec 23, 2015

@sushantdhiman Good question.

mickhansen added a commit that referenced this issue Dec 29, 2015
Fix #4856 : Attributes from multiple scopes doesn't merge
@thatisuday
Copy link

@thatisuday thatisuday commented Mar 1, 2017

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

@alxestevam
Copy link

@alxestevam alxestevam commented Apr 29, 2020

Any updates? I'm having this problem too.

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

No branches or pull requests

6 participants