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

标签 node.js postgresql sequelize.js

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

'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');
    }
};

链接到相应的 VendorStatus :
'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 模型具有以下关联:
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] });

但这给了我错误 column "VendorStatus.name" does not exist 。生成的 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']] }] });

我希望它有帮助!

关于node.js - 使用 sequelize 检索链接状态字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60388370/

相关文章:

python - 如果 Django 中已经存在某些表,如何强制迁移到数据库?

node.js - NodeJS 的 Sequelize 和 hasMany 使用 UUID 错误

node.js - Sequelize - 无法读取未定义的属性 'list'

node.js - 错误 : Cannot find "/config/config.json". 你运行 "sequelize init"了吗?

node.js - 与 nginx 的单个 websocket 连接速率限制

node.js - redis高并发插入

ruby-on-rails - 尝试执行 "FATAL: role "时获取 "docker up"根“不存在”

javascript - 未定义模块名称 - node js

node.js - 错误 : failed to find request token in session

python - Sqlalchemy postgresql JSON 类型字段中的 null 和 None 转换