续集:如何重命名包含中的列

Roo*_*Dev 2 javascript json sequelize.js

我有这个查询:

\n
    const data = await modelOwners.findAll({\n  attributes: ["id", "name", "email"],\n  include: [\n    {\n      model: modelLotsTantiemes,\n      as: "LotsOwner",\n      attributes: [\n        "lotId",\n        ["distributionKeyId", 'costKey'],\n        [ Sequelize.fn('sum', Sequelize.col("tantiemeNumber")), 'totalTantieme' ],\n      ],\n    },\n  ],\n  where: { coproNumber: req.header("customerId")},\n  group: ["name", "LotsOwner.distributionKeyId"],\n  raw: true,\n  nest: false,\n  order: [["name", "ASC"]],\n});\n
Run Code Online (Sandbox Code Playgroud)\n

当我在 Postman Y 上测试它时,结果如下:

\n
"data": [\n    {\n        "id": 4,\n        "name": "Clement Tine",\n        "email": "clementt@fruitsetlegumes.fr",\n        "LotsOwner.field1": 2,\n        "LotsOwner.field2": 1,\n        "LotsOwner.field3": "1892"\n    },\n    {\n        "id": 6,\n        "name": "L\xc3\xa9o Pard",\n        "email": "leopard@thoiry.fr",\n        "LotsOwner.field1": 9,\n        "LotsOwner.field2": 1,\n        "LotsOwner.field3": "4432"\n    }]\n
Run Code Online (Sandbox Code Playgroud)\n

但我想要为我的专栏起一个其他名称,例如:

\n
"data": [\n    {\n        "id": 4,\n        "name": "Clement Tine",\n        "email": "clementt@fruitsetlegumes.fr",\n        "field1": 2,\n        "field2": 1,\n        "field3": "1892"\n    }]\n  \n
Run Code Online (Sandbox Code Playgroud)\n

我不想要关联别名的名称。您知道是否有选择可以做到这一点吗?

\n

Ana*_*oly 5

如果您希望关联模型的属性与主模型处于同一级别,那么您需要手动包含它们以指示关联模型名称:

const data = await modelOwners.findAll({
  attributes: ["id", "name", "email",
  [Sequelize.col('"LotsOwner"."distributionKeyId"'), 'costKey']
  ],
  include: [
    {
      model: modelLotsTantiemes,
      as: "LotsOwner",
...
Run Code Online (Sandbox Code Playgroud)