当我在#findAll中使用order选项时,它会生成SQL:
SELECT
`id`, `first_name` AS `firstName`,
`last_name` AS `lastName` FROM `customers` AS `Customer`
ORDER BY `Customer`.`firstName` DESC;
但是这个SQL会导致错误:
ER_BAD_FIELD_ERROR: Unknown column 'Customer.firstName' in 'order clause'
代码示例:
var Customer = sequelize.define("Customer", {
id: {
type: Sequelize.INTEGER({unsigned: true}),
primaryKey: true
},
firstName: {
type: Sequelize.STRING(32),
field: "first_name"
},
lastName: {
type: Sequelize.STRING(32),
field: "last_name"
}
}, {
name: {
singular: "customer",
plural: "customers"
},
tableName: "customers",
timestamps: false,
underscored: true
});
Customer.findAll({
order: [["firstName", "DESC"]]
}).then(function(list) {
console.log(list);
}).catch(function(err) {
console.log(err);
});
Mysql:5.6.20
Sequelize:3.14.2
这个问题是否有任何解决方案?
答案 0 :(得分:0)
使用order: [["first_name", "DESC"]]
; ORDER BY
查看列名,而不是SELECT
别名firstName
。