在尝试使用Postgresql执行看似有效的Sequelize查询时,获取“多次指定的表名X

bq9c1y66  于 2023-02-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(134)

在这一点上,我不能改变模型--至少不能改变数据库本身。
我得到了以下表格关联:

profile_organization.hasMany(profile, { foreignKey: "organization_id" });
profile.belongsTo(profile_organization, { as: "ProfileOrganization", foreignKey: "organization_id" });

profile_user.hasMany(profile, { foreignKey: "user_account_id" });
profile.belongsTo(profile_user, { as: "ProfileUser", foreignKey: "user_account_id" });

profile_user.belongsToMany(profile_organization, { 
    as: "ProfileOrganizations", 
    through: user_to_organization, 
    foreignKey: "user_id" 
});
profile_organization.belongsToMany(profile_user, { 
    as: "ProfileUsers", 
    through: user_to_organization, 
    foreignKey: "organization_id" 
});

此查询适用于:

let data = await profile_user.findOne({
    where: {
        auth_user_account_id: user.id,
        active: true
    },
    include: [
        {
            model: profile_organization,
            as: "ProfileOrganizations",
            required: false,
            include: [
                {
                    model: profile,
                    required: false,
                    include: [
                        {
                            model: profile_organization,
                            as: "ProfileOrganization",
                            required: false,
                            where: { active: true }
                            // include: [
                            //     {
                            //         model: profile_user,
                            //         as: "OrgProfileUsers",
                            //         required: false
                            //     }
                            // ]
                        }
                    ]
                }
            ]
        }
    ]
}, { raw: true });

...但我还需要它来获取注解掉的关联用户,如下所示:

let data = await profile_user.findOne({
    where: {
        auth_user_account_id: user.id,
        active: true
    },
    include: [
        {
            model: profile_organization,
            as: "ProfileOrganizations",
            required: false,
            include: [
                {
                    model: profile,
                    required: false,
                    include: [
                        {
                            model: profile_organization,
                            as: "ProfileOrganization",
                            required: false,
                            where: { active: true },
                            include: [
                                {
                                    model: profile_user,
                                    as: "ProfileUsers",
                                    required: false
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}, { raw: true });

我用Sequelize做过更奇怪的查询,在层次结构中如此之深,所以我觉得它一定是模型中的关联?
最后我总是说:
多次指定了表名“配置文件组织-〉配置文件-〉配置文件组织-〉配置文件使用

ma8fv8wu

ma8fv8wu1#

您似乎遇到了PostgreSQL标识符限制-最多64个字符。要解决此问题,您需要在Sequelize示例选项中指定minifyAliases: true,请参阅Sequelize示例构造函数

相关问题