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

How to sort by column? #11556

Closed
SGAWP opened this issue Oct 16, 2019 · 1 comment
Closed

How to sort by column? #11556

SGAWP opened this issue Oct 16, 2019 · 1 comment

Comments

@SGAWP
Copy link

SGAWP commented Oct 16, 2019

When I sort by role_name column, this error occurs:

column users.role_name does not exist

If sort by any other column, then there will be no problems. How to sort by column for which join is performed?

module.exports.getAll = async function (req, res) {
    try {
        const query = {
            where: {
                [Op.or]: [
                    {
                        full_name: {
                            [Op.iLike]: `%${req.query.search}%`
                        }
                    },
                    {
                        username: {
                            [Op.like]: `%${req.query.search}%`
                        }
                    },
                    Sequelize.where(Sequelize.col((Roles, "role_name"), "varchar"), {
                        [Op.like]: `%${req.query.search}%`
                    })
                ]
            },
            include: [{
                model: Roles,
                attributes: ["role_name"],
                required: true
            }],
            offset: +req.query.pageSize * (+req.query.page - 1),
            limit: +req.query.pageSize,
            order: [
                [req.query.sort, req.query.order]
            ]
        };
        const users = await Users.findAndCountAll(query);
        res.status(200).json(users);
    } catch (e) {
        errorHandler(res, e);
    }
};
@sushantdhiman
Copy link
Contributor

Please use Github Issue Tracker only for reporting bugs, requesting new features or discussions. Ask questions on Stackoverflow sequelize.js tag or Slack. While I would like to help answer any questions but it still take too much time.

If you are reporting a bug please isolate it as SSCCE, present it nicely and follow issue template. This will help maintainers and contributors understand your issue quickly.

If you are requesting a feature spend some time to properly present your use case with some examples, current and expected outcome.

( What is SSCCE ? You can find template for Sequelize here )

A few good example of SSCCE

  1. ResourceRequest timed out - connection not reestablished after db restarted #8014 (comment)
  2. removeAttribute('id') results in undefined: null data value #7318 (comment)
  3. toJSON converting booleans to "t" (true) or "f" (false) #8749 (comment)

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

No branches or pull requests

2 participants