序列化多对多选择返回联接表属性

时间:2019-10-08 04:01:31

标签: sequelize.js

我有一个由Rails应用程序创建的现有数据库架构,该架构描述了组织和费率计划之间的多对多关系。我正在尝试重新定义节点中的关系并进行排序,并且似乎findOne执行了包含联接表属性的SELECT语句。我确实找到了与此相关的另一篇文章,但是该解决方案似乎无效(Sequelize Join Models Include many to many)。

module.exports = (sequelize, DataTypes) => {
    const Organization = sequelize.define('organizations', {
        id: {
            allowNull: false,
            primaryKey: true,
            type: DataTypes.UUID
        },
        accountType: {
            field: 'account_type',
            type: DataTypes.UUID,
        },
        createdAt: {
            field: 'created_at',
            type: DataTypes.DATE,
        },
        updatedAt: {
            field: 'updated_at',
            type: DataTypes.DATE,
        }
    }, {});
    Organization.associate = function (models) {
        Organization.belongsToMany(models.rate_plans, {
            through: models.organizations_rate_plans,
            as: 'ratePlans',
            foreignKey: 'organization_id',
            otherKey: 'rate_plan_id'
        });
    };
    return Organization;
};
module.exports = (sequelize, DataTypes) => {
    const RatePlan = sequelize.define('rate_plans', {
        id: {
            allowNull: false,
            primaryKey: true,
            type: DataTypes.UUID
        },
        tag: {
            field: 'tag',
            type: DataTypes.STRING,
        },
        createdAt: {
            field: 'created_at',
            type: DataTypes.DATE,
        },
        updatedAt: {
            field: 'updated_at',
            type: DataTypes.DATE,
        },
    }, {});
    RatePlan.associate = function (models) {
        RatePlan.belongsToMany(models.organizations, {
            through: models.organizations_rate_plans,
            as: 'organizations',
            foreignKey: 'rate_plan_id',
            otherKey: 'organization_id'
        });
    };
    return RatePlan;
};

由于我的联接表未遵循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: [] }
        }]
    });

这是要执行的SQL语句:

  

选择   “ organizations”。“ id”,“ organizations”。“ account_type” AS“ accountType”,   “ organisations”。“ created_at” AS“ createdAt”,   “组织”。“ updated_at” AS“ updatedAt”,   “ ratePlans”。“ id” AS“ ratePlans.id”,   “ ratePlans”。“ tag” AS“ ratePlans.tag”,   “” ratePlans-> organizations_rate_plans“。” organization_id“ AS” ratePlans.organizations_rate_plans.organizationId“,   “” ratePlans-> organizations_rate_plans“。” rate_plan_id“ AS” ratePlans.organizations_rate_plans.ratePlanId“,   “” ratePlans-> organizations_rate_plans“。” createdAt“ AS” ratePlans.organizations_rate_plans.createdAt“,   “” ratePlans-> organizations_rate_plans“。” updatedAt“ AS” ratePlans.organizations_rate_plans.updatedAt“,   “ ratePlans-> organizations_rate_plans”。“ organization_id” AS“ ratePlans.organizations_rate_plans.organization_id”,   “ ratePlans-> organizations_rate_plans”。“ rate_plan_id” AS“ ratePlans.organizations_rate_plans.rate_plan_id”   从“组织”到“组织”   左外连接(       “ organizations_rate_plans” AS“ ratePlans-> organizations_rate_plans”       INNER JOIN“ rate_plans” AS“ ratePlans”在“ ratePlans”上。“ id” =“ ratePlans-> organizations_rate_plans”。“ rate_plan_id”)   ON“ organizations”。“ id” =“ ratePlans-> organizations_rate_plans”。“ organization_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)“,” message“:”无法加载组织d7f8b0c7-f530-4f6b-9862-76ac440af868。SequelizeDatabaseError:列ratePlans-> organizations_rate_plans.createdAt确实存在不存在”,“名称”:“错误”}

不应选择连接模型列名称(特别是因为我在findOne中添加了attribute = [])。而且,它们有点奇怪。看来它们要么是费率计划的列,要么是组织的列。我仍然不确定我是否正确定义了多对多关系。

1 个答案:

答案 0 :(得分:0)

这似乎是Sequelize( https://github.com/sequelize/sequelize/issues/5481)中的一个已知问题。这不是我期望从Rails看到的,必须要做的,但是我没有访问organization.rate_plans,而是走了另一条路线:rate_plans.where(organization_id = ...)。通过:{属性:[]}与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: [] }
        }]
    });
}