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

through: { where: condition } is working? Is this in the docs? scope not working #9775

Closed
onadeem1 opened this issue Aug 8, 2018 · 3 comments
Labels

Comments

@onadeem1
Copy link

onadeem1 commented Aug 8, 2018

What are you doing?

//User Model
module.exports = db => {
  const User = db.define(
    'user',
    {
      firstName: {
        type: STRING
      },
    },
    {
      scopes: {
        friends: function() {
          return {
            through: {
              where: {
                accepted: true
              }
            }
          }
        }
      }
    }
  )
  return User
}

module.exports.associations = (User ) => {
  User.belongsToMany(User, { through: Friendship, as: 'friends' })
}

//Friendship Model
module.exports = db => {
  const Friendship = db.define(
    'friendship',
    {
      accepted: {
        type: BOOLEAN,
        defaultValue: false
      },
      originalRequest: {
        type: BOOLEAN,
        defaultValue: false
      }
    },
  )
  return Friendship
}

//this works correctly and returns only friends where accepted = true
router.get(
  '/',
  asyncHandler(async (req, res, next) => {
    const friends = await req.requestedUser.getFriends({
      through: { where: { accepted: true } },
      logging: console.log
    })
    res.json(friends)
  })
)

//this does not work
router.get(
  '/',
  asyncHandler(async (req, res, next) => {
    const friends = await req.requestedUser.getFriends({
      scope: 'friends',
      logging: console.log
    })
    res.json(friends)
  })
)

What do you expect to happen?

Hello all I am trying to query based on a condition from the through table where Users are related to Users through friendship. It seems to work correctly but I couldn't find this option in the docs anywhere. Is this a new feature or can someone link to where it is mentioned? I was trying to apply a scope which would return a query from the through table but that does not work correctly. Any reason this would work one way but not the other?

What is actually happening?

The query is working with the through condition but not with the scope. Looking for information on querying the through table based on association methods & also curious how to apply scope to a through table.

Dialect: Postgres
Sequelize version: 5.0.0-beta.10
Tested with latest release: Yes (If yes, specify that version)

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

@stale
Copy link

stale bot commented Nov 6, 2018

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 🙂

@stale stale bot added the stale label Nov 6, 2018
@doc82
Copy link

doc82 commented Nov 6, 2018

I am having the same issue with PG v7 and any sequelize version

@stale stale bot removed the stale label Nov 6, 2018
@stale
Copy link

stale bot commented Feb 4, 2019

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 🙂

@stale stale bot added the stale label Feb 4, 2019
@stale stale bot closed this as completed Feb 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants