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 · 8 comments
Closed

Scopes attributes are not merged #4856

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

Comments

@lifenautjoe
Copy link

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

@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

Yep, agree :)

@sushantdhiman
Copy link
Contributor

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

@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 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 commented Apr 29, 2020

Any updates? I'm having this problem too.

@math1974
Copy link

There are any updates on this issue? Still facing this problem

@ephys
Copy link
Member

ephys commented Jul 13, 2023

Please open a new issue with a description of your problem, this thread is extremely old and outdated

@sequelize sequelize locked as resolved and limited conversation to collaborators Jul 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants