Sequelize.js sequelize多对多选择返回联接表属性

Sequelize.js sequelize多对多选择返回联接表属性,sequelize.js,Sequelize.js,我有一个由Rails应用程序创建的现有db模式,它描述了组织和费率计划之间的多对多关系。我试图在node和sequelize中重新定义关系,findOne似乎执行了一个包含联接表属性的SELECT语句。我确实找到了另一篇关于这个的帖子,但是这个解决方案似乎不起作用() 由于我的联接表不遵循sequelize命名约定,我想我必须定义它,对吗?另外,请注意,此表上没有id PK,只有2个FK module.exports = (sequelize, DataTypes) => { co

我有一个由Rails应用程序创建的现有db模式,它描述了组织和费率计划之间的多对多关系。我试图在node和sequelize中重新定义关系,findOne似乎执行了一个包含联接表属性的SELECT语句。我确实找到了另一篇关于这个的帖子,但是这个解决方案似乎不起作用()

由于我的联接表不遵循sequelize命名约定,我想我必须定义它,对吗?另外,请注意,此表上没有id PK,只有2个FK

module.exports = (sequelize, DataTypes) => {
    const OrganizationRatePlan = sequelize.define('organizations_rate_plans', {
        id: {
            primaryKey: true,
            field: 'id',
            type: DataTypes.INTEGER,
        },
        organizationId: {
            field: 'organization_id',
            type: DataTypes.UUID,
        },
        ratePlanId: {
            field: 'rate_plan_id',
            type: DataTypes.UUID,
        },
    }, {});
    return OrganizationRatePlan;
};
我的问题是:

    const organization = await db.organizations.findOne({
        where: { id: organizationId },
        include: [{
            model: db.rate_plans,
            as: 'ratePlans',
            through: { model: db.organizations_rate_plans, attributes: [] }
        }]
    });
下面是获取execute的SQL语句:

挑选 “组织”“id”“组织”“帐户类型”“作为“帐户类型”, “组织”。“创建时间”为“创建日期”, “组织”。“更新地点”为“更新地点”, “ratePlans”。“id”作为“ratePlans.id”, “ratePlans”。“标记”为“ratePlans.tag”, “ratePlans->organizations\u rate\u plans”.“organization\u id”为“ratePlans.organizations\u rate\u plans.organizationId”, “ratePlans->organizations\u rate\u plans”.“rate\u plan\u id”为“ratePlans.organizations\u rate\u plans.ratePlanId”, “ratePlans->organizations\u rate\u plans”。“createdAt”作为“ratePlans.organizations\u rate\u plans.createdAt”, “ratePlans->organizations\u rate\u plans”.“updatedAt”为“ratePlans.organizations\u rate\u plans.updatedAt”, “ratePlans->organization\u rate\u plans”.“organization\u id”为“ratePlans.organization\u rate\u plans.organization\u id”, “ratePlans->organizations\u rate\u plans.”“rate\u plan\u id”为“ratePlans.organizations\u rate\u plans.rate\u plan\u id” 从“组织”变为“组织” 左外联接( “组织评分计划”为“评分计划->组织评分计划” 将“费率计划”内部联接为“费率计划”中的“费率计划”。“id”=“费率计划->组织费率计划”。“费率计划id”) 在“组织”上,“id”=“费率计划->组织费率计划”“组织id” 其中“组织”。“id”=“d7f8b0c7-f530-4f6b-9862-76ac440af868”

我得到的错误是:

{“stack”:“错误:无法加载组织d7f8b0c7-f530-4f6b-9862-76ac440af868。SequelizeDatabaseError:列ratePlans->organizations_rate_plans.createdAt在OrganizationRepository.loadById(/usr/src/app/src/persistence/db/organization repository.js:13:19)”中不存在\n创建数据,“消息”:“未能加载组织d7f8b0c7-f530-4f6b-9862-76ac440af868。SequelizeDatabaseError:列ratePlans->organizations_rate_plans.createdAt不存在”,“name:”错误“}”


不应选择联接模型列名称(特别是因为我将属性=[]添加到findOne)。此外,它们有点奇怪。它们似乎是费率计划的列或组织的列。我仍然不确定是否正确定义了多对多关系。

这似乎是Sequelize中的一个已知问题(和。)。这不是我期望从Rails看到的,也不是我必须做的,而是访问organization.rate_plans,我走了另一条路线:rate_plans。其中(organization_id=…)。through:{attributes:[]}与includeIgnoreAttributes:false是多余的

async function findOrganizationById(organizationId) {
    return await db.organizations.findOne({
        where: { id: organizationId },
        includeIgnoreAttributes: false,
        include: [{
            model: db.rate_plans,
            as: 'ratePlans',
            through: { model: db.organizations_rate_plans, attributes: [] }
        }]
    });
}
    const organization = await db.organizations.findOne({
        where: { id: organizationId },
        include: [{
            model: db.rate_plans,
            as: 'ratePlans',
            through: { model: db.organizations_rate_plans, attributes: [] }
        }]
    });
async function findOrganizationById(organizationId) {
    return await db.organizations.findOne({
        where: { id: organizationId },
        includeIgnoreAttributes: false,
        include: [{
            model: db.rate_plans,
            as: 'ratePlans',
            through: { model: db.organizations_rate_plans, attributes: [] }
        }]
    });
}