Model Include return 属性的选项作为具有定义别名的单个值而不是具有属性的对象
Model Include option to return attributes as single value with defined alias instead of an object with properties
我有 2 个模型:
Property
Account
A Property
hasOne
Account
属性
Property.belongsTo(models.Account, {
as: 'account',
foreignKey: 'accountNumber'
});
账户
Account.hasOne(models.Property, {
as: 'property',
foreignKey: 'accountNumber'
});
在 findAll
查询中我有
const properties = await Property.findAll({
attributes: ['accountNumber'],
include: [
{
model: Models.Account,
as: 'account',
attributes: ['organisation', 'email'],
},
]
});
这 returns 每个项目一个对象;
{
"accountNumber":"AC0012",
"account":{
"organisation":"Example Org",
"email":"email@email.com"
}
}
然而,我的目标是,
{
"accountNumber":"AC0012",
"accountOrganisation":"Example Org",
"accountEmail":"email@email.com"
}
当前MySQL查询如下;
SELECT `Property`.`id`,
`Property`.`account_number` AS `accountNumber`,
`account`.`account_number` AS `account.accountNumber`,
`account`.`organisation` AS `account`.`organisation`,
`account`.`email` AS `account.email`
FROM `property_dev`.`property`
AS `Property`
LEFT OUTER JOIN `property_dev`.`account` AS `account`
ON `Property`.`account_number` = `account`.`account_number`
我需要更新使用的别名;
`account`.`organisation` AS `account`.`organisation`,
`account`.`email` AS `account.email`
到
`account`.`organisation` AS `accountOrganisation`,
`account`.`email` AS `accountEmail`
我怎样才能做到这一点?看起来很简单,但我似乎无法查询正确的解决方案。我可能在搜索中使用了不正确的术语,浏览官方文档并没有引导我找到解决方案。
如有任何帮助,我们将不胜感激
您可以使用带有 [value, key]
的数组为连接的列添加别名,其中值是包含模型的 sequelize.col()
值。由于您只需要原始 JSON 结果,您还可以传入 raw: true
以不将结果解析为模型实例以获得更好的性能。
const properties = await Property.findAll({
attributes: [
'accountNumber',
[sequelize.col('account.organisation'), 'accountOrganisation'],
[sequelize.col('account.email'), 'accountEmail'],
],
include: {
model: Models.Account,
as: 'account',
attributes: [],
},
raw: true,
});
我有 2 个模型:
Property
Account
A Property
hasOne
Account
属性
Property.belongsTo(models.Account, {
as: 'account',
foreignKey: 'accountNumber'
});
账户
Account.hasOne(models.Property, {
as: 'property',
foreignKey: 'accountNumber'
});
在 findAll
查询中我有
const properties = await Property.findAll({
attributes: ['accountNumber'],
include: [
{
model: Models.Account,
as: 'account',
attributes: ['organisation', 'email'],
},
]
});
这 returns 每个项目一个对象;
{
"accountNumber":"AC0012",
"account":{
"organisation":"Example Org",
"email":"email@email.com"
}
}
然而,我的目标是,
{
"accountNumber":"AC0012",
"accountOrganisation":"Example Org",
"accountEmail":"email@email.com"
}
当前MySQL查询如下;
SELECT `Property`.`id`,
`Property`.`account_number` AS `accountNumber`,
`account`.`account_number` AS `account.accountNumber`,
`account`.`organisation` AS `account`.`organisation`,
`account`.`email` AS `account.email`
FROM `property_dev`.`property`
AS `Property`
LEFT OUTER JOIN `property_dev`.`account` AS `account`
ON `Property`.`account_number` = `account`.`account_number`
我需要更新使用的别名;
`account`.`organisation` AS `account`.`organisation`,
`account`.`email` AS `account.email`
到
`account`.`organisation` AS `accountOrganisation`,
`account`.`email` AS `accountEmail`
我怎样才能做到这一点?看起来很简单,但我似乎无法查询正确的解决方案。我可能在搜索中使用了不正确的术语,浏览官方文档并没有引导我找到解决方案。
如有任何帮助,我们将不胜感激
您可以使用带有 [value, key]
的数组为连接的列添加别名,其中值是包含模型的 sequelize.col()
值。由于您只需要原始 JSON 结果,您还可以传入 raw: true
以不将结果解析为模型实例以获得更好的性能。
const properties = await Property.findAll({
attributes: [
'accountNumber',
[sequelize.col('account.organisation'), 'accountOrganisation'],
[sequelize.col('account.email'), 'accountEmail'],
],
include: {
model: Models.Account,
as: 'account',
attributes: [],
},
raw: true,
});