【问题标题】:Sequelize PostgreSQL alphabetical sortSequelize PostgreSQL 字母排序
【发布时间】:2021-05-25 12:59:19
【问题描述】:

我有一张产品表

id | code | description
---------------------------------------
1  | 10   | description for product 10
2  | 2    | description for product 2
3  | 1    | description for product 1

这是续集模型

module.exports = class Product extends Model {
  static init(sequelize) {
    return super.init({
      id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        unique: true,
        primaryKey: true
      },
      code: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true
      },
      description: {
        type: DataTypes.STRING,
        allowNull: true,
        unique: false
      }
    },{
      sequelize,
      modelName: 'Product',
      tableName: 'products'
    })
  }
}

我正在尝试使用代码字母数字顺序检索产品,但我得到的是字典顺序,而不是 1 -> 2 -> 10 我得到的是 1 -> 10 -> 2

function getAll(req, res, next) {
  models.Products.findAll({
    where: {},
    order: [
        ['code', 'ASC']
    ]
  })
  .then(products => {
    res.send(products)
  })
}

我看到在 postgres 中我可以将字段转换为一个

SELECT code
FROM products
ORDER BY code::bytea;

但我不确定sequelize是否可以这样做

更新

我能够使用所选答案进行修复

function getAll(req, res, next) {
  models.Products.findAll({
    attributes: [
      'id',
      'code',
      [models.sequelize.literal(`case when code ~ '^[0-9]*$' then code::integer else null end`), 'sort_code'], 
      'description'
    ]
    where: {},
    order: [
      [models.sequelize.col('sort_code'), 'ASC'],
      ['code', 'ASC']
  ]
  })
  .then(products => {
    res.send(products)
  })
}

【问题讨论】:

  • 您将code 定义为字符串而不是整数是否有令人信服的理由?
  • @MikeSherrill'CatRecall' 感谢您的评论,我没有添加到示例中,但是还有一些其他代码以字母开头,后跟一系列字符

标签: postgresql sorting sequelize.js alphanumeric


【解决方案1】:

在 SQL 中,您可以使用 CASE...END 表达式控制排序顺序。做出一些假设。 . .

create table products (
    id integer primary key,
    code varchar(5) not null unique,
    description varchar(30) null
);


insert into products values
(1, 10,     'description for product 10'),
(2, 2,      'description for product 2'),
(3, 1,      'description for product 1'),
(4, 'A10',  'description for product A10'),
(5, 20,     'description for product 20'),
(6, 99,     'description for product 99'),
(7, 21,     'description for product 21'),
(8, 'RA10', 'description for product R10');


select id, 
       code, 
       case when code ~ '^\d*$' then code::integer else null end as sort_code, 
       description
from products
order by sort_code, code;


id  code  sort_code  description
--
3   1     1          description for product 1
2   2     2          description for product 2
1   10    10         description for product 10
5   20    20         description for product 20
7   21    21         description for product 21
6   99    99         description for product 99
4   A10              description for product A10
8   RA10             description for product RA10

我不再使用 Sequelize,但如果我有时间,我会尝试稍后翻译。

【讨论】:

  • 当我执行查询时它工作了,我一直试图让它在sequelize中工作,它几乎可以工作,但它在代码行中不起作用models.sequelize.literal(case ~ '^\ d*$' ...) javascript 字符串打破了正则表达式,由于某种原因^\d*$ 我无法转义字符 \,我明天继续,谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-08-08
  • 2012-07-10
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多