[英]sequelize : how rename column in include
我有這個查詢:
const data = await modelOwners.findAll({
attributes: ["id", "name", "email"],
include: [
{
model: modelLotsTantiemes,
as: "LotsOwner",
attributes: [
"lotId",
["distributionKeyId", 'costKey'],
[ Sequelize.fn('sum', Sequelize.col("tantiemeNumber")), 'totalTantieme' ],
],
},
],
where: { coproNumber: req.header("customerId")},
group: ["name", "LotsOwner.distributionKeyId"],
raw: true,
nest: false,
order: [["name", "ASC"]],
});
當我在 Postman Y 上測試時,結果如下:
"data": [
{
"id": 4,
"name": "Clement Tine",
"email": "clementt@fruitsetlegumes.fr",
"LotsOwner.field1": 2,
"LotsOwner.field2": 1,
"LotsOwner.field3": "1892"
},
{
"id": 6,
"name": "Léo Pard",
"email": "leopard@thoiry.fr",
"LotsOwner.field1": 9,
"LotsOwner.field2": 1,
"LotsOwner.field3": "4432"
}]
但我想為我的專欄取一個其他名稱,例如:
"data": [
{
"id": 4,
"name": "Clement Tine",
"email": "clementt@fruitsetlegumes.fr",
"field1": 2,
"field2": 1,
"field3": "1892"
}]
我不想要關聯別名的名稱。 您知道是否可以選擇這樣做嗎?
如果您希望關聯 model 的屬性與主 model 在同一級別,則需要手動包含它們以指示關聯的 model 名稱:
const data = await modelOwners.findAll({
attributes: ["id", "name", "email",
[Sequelize.col('"LotsOwner"."distributionKeyId"'), 'costKey']
],
include: [
{
model: modelLotsTantiemes,
as: "LotsOwner",
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.