【问题标题】:Sequelize - Get values in descending orderSequelize - 按降序获取值
【发布时间】:2017-09-21 21:17:42
【问题描述】:

我正在使用 sequelize 连接到 mysql 数据库。 我的数据库中有 2 个表 ApplicationVersion 和 Provider。这两个表都是关联的。 ApplicationVersion 将 Provider 'uuid' 作为外键。

在我的 Provider 表中,我有一个名为 weight 的列。此权重字段具有整数值。我想按降序显示元素。所以我在我的函数中使用了这条线order: [Sequelize.col('weight'), 'DESC'],,但它不起作用。如果我从中删除订单,该功能运行良好。

函数如下:

function getactiveProvidersList(user, callback) {
        Provider.findAll({where: {enabled: 1},
            include: [{
                    model: ApplicationVersion,
                    where: {'active': true},
                }],
                order: [Sequelize.col('weight'), 'DESC'],
            }).then(function(providers) {
                 callback(providers);
                });
    }

您能否告诉我正确的做法以及我在哪里搞砸了。

任何建议都是有帮助的。谢谢!

【问题讨论】:

    标签: javascript sql node.js sequelize.js


    【解决方案1】:

    试试这个。

    function getactiveProvidersList(user, callback) {
                Provider.findAll({where: {enabled: 1},
                    include: [{
                            model: ApplicationVersion,
                            where: {'active': true},
                        }],
                        order: ['weight','DESC'],
                    }).then(function(providers) {
                         callback(providers);
                        });
            }
    

    在此处以及页面底部有更多关于此的建议

    http://docs.sequelizejs.com/manual/tutorial/querying.html

    【讨论】:

    • 两个都试过了,但都不管用。它说Provider.Literal 不是函数,Provider.fn 不是函数
    • order: [ ['weight', 'DESC'],], 这对我有用。谢谢@Legman
    • 我试过了,顺序不行:[['weight', 'DESC'],],而是
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 2017-12-21
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多