gpt4 book ai didi

javascript - sequelize.literal 返回原属性名称和重命名后的属性

转载 作者:行者123 更新时间:2023-12-03 22:33:06 25 4
gpt4 key购买 nike

我想在执行 join 后重命名属性,我尝试在 nodejs 中使用 sequelize.literal 但它返回原始名称和修改后的名称。如何从结果中删除原点名称?

const result = await User.findAll({
attributes: [
'name',
'id',
[Sequelize.literal('Company.name'), 'companyName'],
[Sequelize.literal('Employee.type'), 'employeeType'],
],
include: [
{ model: Company, attributes: ['name'], required: false },
{ model: Employee, attributes: ['type'], required: true },,
],
raw: true,
});

结果:
...
"companyName": "Test",
"employeeType": "Junior",
"Company.name": "Test",
"Employee.type": "Junior",
预期结果:
...
"companyName": "Test",
"employeeType": "Junior",

最佳答案

我刚刚找到了这个问题的解决方案,但我对其他想法持开放态度
这就是我所做的,它对我很有效。

const result = await User.findAll({
attributes: [
'name',
'id',
'Company.name',
[Sequelize.literal('`Company`.`name`'), 'companyName'],
'Employee.type',
[Sequelize.literal('`Employee`.`type`'), 'employeeType'],
],
include: [
{ model: Company, attributes: [], required: false },
{ model: Employee, attributes: [], required: true },,
],
raw: true,
});

关于javascript - sequelize.literal 返回原属性名称和重命名后的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64885281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com