Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用sequelize检索链接状态字段的值_Node.js_Postgresql_Sequelize.js - Fatal编程技术网

Node.js 使用sequelize检索链接状态字段的值

Node.js 使用sequelize检索链接状态字段的值,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,我有一个带有statusId字段的Vendor实体: 'use strict'; module.exports = { up: (queryInterface, Sequelize) => { return queryInterface.createTable('Vendors', { id: { allowNull: false, autoIncrement: true,

我有一个带有
statusId
字段的
Vendor
实体:

'use strict';
module.exports = {
    up: (queryInterface, Sequelize) => {
        return queryInterface.createTable('Vendors', {
            id: {
                allowNull: false,
                autoIncrement: true,
                primaryKey: true,
                type: Sequelize.INTEGER
            },
            // other properties
            statusId: {
                type: Sequelize.INTEGER,
                references: {
                    model: 'VendorStatuses',
                    key: 'id'
                },
                onUpdate: 'CASCADE',
                onDelete: 'SET NULL'
            },
            // other properties
        });
    },
    down: (queryInterface, Sequelize) => {
        return queryInterface.dropTable('Vendors');
    }
};
它链接到相应的
供应商状态

'use strict';
module.exports = {
    up: (queryInterface, Sequelize) => {
        return queryInterface.createTable('VendorStatuses', {
            id: {
                allowNull: false,
                autoIncrement: true,
                primaryKey: true,
                type: Sequelize.INTEGER
            },
            name: {
                type: Sequelize.STRING
            },
        });
    },
    down: (queryInterface, Sequelize) => {
        return queryInterface.dropTable('VendorStatuses');
    }
};
供应商
模型具有以下关联:

Vendor.belongsTo(models.VendorStatus, { foreignKey: 'statusId'})
如何让Sequelize生成以下等效项:

SELECT "Vendor"."id", "Vendor"."name", 
    "VendorStatus"."name" AS "status"
FROM "Vendors" AS "Vendor"
    LEFT OUTER JOIN "VendorStatuses" AS "VendorStatus"
    ON "Vendor"."statusId" = "VendorStatus"."id";
基本上,我希望在返回给客户机的JSON结果中,
status
字段填充
VendorStatus.name

我试过:

    const attribs = ['id', 'name', 'email', ['VendorStatus.name', 'status']];
    const vendors = await models.Vendor.findAll({ attributes: attribs, include: [models.VendorStatus] });
但是这给了我一个错误
列“VendorStatus.name”不存在
。生成的SQL是:

SELECT "Vendor"."id", 
       "Vendor"."name", 
       "Vendor"."email", 
       "vendorstatus.name"   AS "status", 
       "VendorStatus"."id"   AS "VendorStatus.id", 
       "VendorStatus"."name" AS "VendorStatus.name" 
FROM   "vendors" AS "Vendor" 
       LEFT OUTER JOIN "vendorstatuses" AS "VendorStatus" 
                    ON "Vendor"."statusid" = "VendorStatus"."id"
版本包括:

[Node: 12.14.1, CLI: 5.5.1, ORM: 5.21.3]

您可以尝试使用以下代码将
VendorStatus
表的name属性设置为
status

const vendors = await models.Vendor.findAll({ attributes: ['id', 'name', 'email'], include: [{ model: models.VendorStatus, attributes: [['name', 'status']] }] });

我希望有帮助

谢谢你的回答,它让我接近了我想要的:
“VendorStatus.status”:“UNAPPROVED”
,但不是全部(如果不编写原始查询,这可能是不可能的)。