我有 2 個型號:PropertyAccountAProperty 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'], }, ]});這會為每個項目返回一個對象,例如;{ "accountNumber":"AC0012", "account":{ "organisation":"Example Org", "email":"[email protected]" }}然而,我的目標是實現這樣的目標:{ "accountNumber":"AC0012", "accountOrganisation":"Example Org", "accountEmail":"[email protected]"}當前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` 我怎樣才能實現這個目標?這看起來很簡單,但我似乎無法查詢正確的解決方案。我可能在搜索中使用了不正確的術語,瀏覽官方文檔并沒有找到解決方案。任何幫助將不勝感激
模型包含選項可將屬性返回為具有定義別名的單個值,而不是具有屬性的對象
一只甜甜圈
2023-08-18 13:58:22