使用顺序选项时,Sequelize 生成无效的 SQL
Sequelize generates a not working SQL when use order option
当我在 #findAll 中使用订单选项时,它会生成 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
续集:3.14.2
这个问题有什么解决方案吗?
使用order: [["first_name", "DESC"]]
; ORDER BY
查看列名,而不是您的 SELECT
别名 firstName
。
当我在 #findAll 中使用订单选项时,它会生成 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
续集:3.14.2
这个问题有什么解决方案吗?
使用order: [["first_name", "DESC"]]
; ORDER BY
查看列名,而不是您的 SELECT
别名 firstName
。