Skip to content

using scopes removes foreign keys? #6245

Closed

Description

@yocontra

What you are doing?

const scopes = {
  public: {
    attributes: {
        exclude: ['secret']
    }
  }
}

// imagine i made a model named User with two fields:
// - secret
// - name
User.belongsTo(City) // user now has cityId field

User.scope('public').findById(123).then((user) =>
  // user === { id: 123, name: 'Bob' }
)

What do you expect to happen?

The foreign key cityId should exist on the result

What is actually happening?

The foreign key cityId does not exist on the result

When not using scopes, cityId appears in the result as expected.

Dialect: postgres
Sequelize version: 3.23.4

Activity

  1. mickhansen commented on Jul 8, 2016

    @mickhansen
    Contributor
  2. janmeier commented on Jul 8, 2016

    @janmeier
    Member

    I'm pretty sure this happens because scopes are converted from attributes: { include, exclude } to attributes: [..] when the model is defined - at which point the foreign keys have not been added yet

  3. yocontra commented on Jul 9, 2016

    @yocontra
    ContributorAuthor

    @janmeier That would explain it if that's how things are implemented in sequelize

  4. yocontra commented on Sep 12, 2016

    @yocontra
    ContributorAuthor

    SSCE:

    var Sequelize = require('./index');
    var sequelize = require('./test/support').createSequelizeInstance();
    
    // boilerplate
    const City = sequelize.define('city', {
      name: Sequelize.STRING
    })
    
    const User = sequelize.define('user', {
      name: Sequelize.STRING,
      secret: Sequelize.STRING
    }, {
      scopes: {
        public: {
          attributes: {
            exclude: ['secret']
          }
        }
      }
    })
    User.belongsTo(City)
    
    const makeCity = () => City.create({ name: 'Test' })
    const makeUser = () =>
      makeCity().then((city) =>
        User.create({
          name: 'Bob',
          city: city
        })
      )
    
    
    // actual problem, code exactly the same as first comment
    makeUser().then((ourUser) => {
      User.scope('public').findById(ourUser.id).then((user) =>
        // user === { id: 123, name: 'Bob' }
      )
    })

    For the record, it's really cumbersome to go through this SSCE (what does this even stand for?) rigmarole for an issue this simple to repro - might be worth revisiting this policy. Seems like a serious deterrent for people who want to report bugs and contribute on issues.

  5. yocontra commented on Nov 3, 2016

    @yocontra
    ContributorAuthor

    Any word on this? Didn't get a response - pretty major bug.

  6. felixfbecker commented on Nov 3, 2016

    @felixfbecker
    Contributor

    @contra Could you try first defining the model, then associating, and then using addScope() to add the scope?

  7. yocontra commented on Nov 3, 2016

    @yocontra
    ContributorAuthor

    @felixfbecker That fixes it

  8. felixfbecker commented on Nov 3, 2016

    @felixfbecker
    Contributor

    Great. The only real fix I think would be to store the scopes raw and normalize JIT, but that might have have a small performance impact.

  9. yocontra commented on Nov 12, 2016

    @yocontra
    ContributorAuthor

    @felixfbecker Sounds good to me

  10. tonivj5 commented on Jan 23, 2017

    @tonivj5

    I use a function instead of objects and it works 👍

    An example:

    scopes: {
        teachers: () => ({
            where: {
                rol: TEACHER,
            },
        }),
        public: () => ({
            attributes: {
                exclude: ['password'],
            },
        }),
    },
  11. scottbeacher commented on May 1, 2017

    @scottbeacher

    I'm having this issue when trying to define a defaultScope that uses exclude in attributes. @felixfbecker is there a way to use addScope to modify the defaultScope? Trying to override it with addScope('defaultScope') doesn't seem to work.

  12. DMan577 commented on May 1, 2017

    @DMan577

    You need to pass {override: true} to the 3rd parameter of addScope to override it.
    addScope('defaultScope', {...}, {override:true}

  13. scottbeacher commented on May 1, 2017

    @scottbeacher

    That worked, thank you.

  14. stale commented on Jun 30, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  15. 2 remaining items

  16. stale commented on Aug 30, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  17. yocontra commented on Aug 30, 2017

    @yocontra
    ContributorAuthor

    Still an issue.

  18. stale commented on Sep 29, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  19. tonivj5 commented on Sep 29, 2017

    @tonivj5

    Still an issue.

  20. stale commented on Dec 28, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  21. yocontra commented on Dec 28, 2017

    @yocontra
    ContributorAuthor

    Keep it open. Can we get any response here to get rid of this stale bot? I've bumped this for months and the ticket has been open for 1.5 years.

  22. added
    type: bug
    on Jan 10, 2018
  23. sushantdhiman commented on Oct 28, 2018

    @sushantdhiman
    Contributor

    Fixed by 88a340d

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions