顺序选择仅选择的属性

Mar*_*cin 3 mysql select model express sequelize.js

我在执行以下操作时正在使用MySQL数据库:

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

        }).then(function (tenants) {

        });
Run Code Online (Sandbox Code Playgroud)

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

我该如何预防?有时我只想选择2/3列,而Sequelize总是选择所有这些列,但效率不高。

Bha*_*san 5

您可以执行以下操作

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) {

    });
Run Code Online (Sandbox Code Playgroud)