Sequelize仅选择所选属性

问题描述 投票:3回答:1

我正在使用MySQL数据库:

models.modelA.findAll({
            attributes: [
               ['modelA.id','id']
            ],
            raw: true,
            include:
                [
                    {
                        model: models.modelB,
                        required: true
                    }
                ]

        }).then(function (tenants) {

        });

尽管如此,我只选择了id,Sequelize也从相关表格中检索所有属性,所以我得到{id,...这里的所有属性}。

我怎么能阻止这个?有时我想只选择2/3列,而Sequelize总是选择所有不高效的列。

mysql express select model sequelize.js
1个回答
4
投票

您可以执行以下操作

models.modelA.findAll({
        attributes: [
           'id'
        ],
        raw: true,
        include:
            [
                {
                    model: models.modelB,
                    attributes: ['fieldName1', 'fieldName2'], // Add column names here inside attributes array.
                    required: true
                }
            ]

    }).then(function (tenants) {

    });
© www.soinside.com 2019 - 2024. All rights reserved.