SequelizeJS:使用数字作为字符串时列值的错误顺序 [英] SequelizeJS: Wrong order of Column Values when using Numbers as String

查看:39
本文介绍了SequelizeJS:使用数字作为字符串时列值的错误顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SequelizeJS 的新手,并将其用于带有 NodeJS 应用程序的 PostgreSQL.

I am new to SequelizeJS and using it for PostgreSQL with NodeJS application.

我有一张桌子:

sequelize.define('log', {
    id: {
        type: type.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    statusCode: type.INTEGER,
    status: type.STRING,
    message: type.TEXT,
    lastRecordId: type.STRING,
    lastRecordTime: type.DATE
});

问题是,当我运行查询以从 lastRecordId 列中以 DESC 的顺序获取值时,我得到了错误的值顺序:

The problem is that, when I run a query for fetching the values from lastRecordId column in DESC order, I got wrong order of the values:

我不想在该列上使用 INTEGERBIGINT,因为它包含的代码不是实数.

I did not want to use INTEGER nor BIGINT on that column, because it contains a code not a real number.

我使用的查询是:

        LoggerModel
            .findAll({
                order: [ [ 'lastRecordId', 'DESC' ]],
            })
            .then( allLogs => {
                //...
            })

推荐答案

我通过使用@bereket 的答案和答案 这里.对我有用的方法如下:

I have found a solution by using the answers of @bereket and from the answer here. What worked for me is as follows:

    LoggModel
        .findAll({
            order: [
                [ sequelize.cast(sequelize.col('lastRecordId'), 'BIGINT') , 'DESC' ]
            ]
        })
        .then((logs) => { /// })

这篇关于SequelizeJS:使用数字作为字符串时列值的错误顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆