简体   繁体   English

按列名顺序排序

[英]Sequelize order by column name

I am trying to order by column name in Sequelize 我正在尝试按Sequelize中的列名排序

Here is my model 这是我的模特

var Milestones = sequelize.define('milestones', {
  milestoneId: Sequelize.INTEGER,
  data: Sequelize.JSON,
  repository: Sequelize.STRING
});

Database is PostegreSql 9.5 数据库是PostegreSql 9.5

var dialect = 'postgres',
    protocol = 'postgres';

var sequelize = new Sequelize(dbConfig.database, dbConfig.user, dbConfig.password, {
  host: dbConfig.host,
  dialect: dialect,
  protocol: protocol,
  dialectOptions: {
    timeout: 30
  },
  pool: {
    max: 5,
    min: 0,
    idle: 30000,
    maxConnections: 5,
    maxIdleTime: 30
  },
  logging: false
});

Here is my query 这是我的查询

models.Milestones.findAll({
      where: {
        repository: req.body.repository
      },
      order: 'milestoneId'
    }).then(function (values) {
      // do something
    });

Here is my error 这是我的错误

Unhandled rejection SequelizeDatabaseError: column "milestoneid" does not exist

So problem is that I wanna order by milestoneId but Sequelize is actually trying to order by milestoneid (notice the lack of camel case). 所以问题是我想按milestoneId排序,但Sequelize实际上是在尝试按milestoneid排序(请注意,缺少骆驼的情况)。

If I change to order by repository everything works, so the issue at this point seems to be related to the conversion to lowercase by Sequelize. 如果我按repository更改顺序,那么一切正常,因此此时的问题似乎与Sequelize转换为小写字母有关。

Any suggestions other than to rename the column in database to lowercase? 除了将数据库中的列重命名为小写以外,还有其他建议吗?

Thank you 谢谢

Update your model with a field and it will be fine 使用字段更新模型,就可以了

var Milestones = sequelize.define('milestones', {
  milestoneId: {
    type: Sequelize.INTEGER,
    field: 'milestoneId'
  },
  data: Sequelize.JSON,
  repository: Sequelize.STRING
});

EDIT: 编辑:

also update the ordering with 也更新订购

order: ['milestoneId']

I had a similar issue with PostGreSQL and Oracle (with Sequelize-Oracle). 我在PostGreSQL和Oracle(使用Sequelize-Oracle)中遇到了类似的问题。 Sequelize automatically adds quotes for many parameters. Sequelize自动为许多参数添加引号。

Do you have "quoteIdentifiers" option turned off ? 您是否已关闭“ quoteIdentifiers”选项? (default is on). (默认启用)。

Maybe the problem isn't : 也许问题不在于:
- order by milestoneid or order by milestoneId -按里程碑编号排序或按MilestoneId排序
But instead it can be : 但是它可以是:
- order by 'milestoneid' (with quotes causing the issue). -按“ milestoneid”排序(使用引号引起问题)。

The doc says : 医生说:

[options.quoteIdentifiers=true] : Boolean, Set to false to make table names and attributes case-insensitive on Postgres and skip double quoting of them. [options.quoteIdentifiers = true]:布尔值,设置为false以使表名和属性在Postgres上不区分大小写,并跳过它们的双引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM