【问题标题】:Using columns from junction table in "where" clause in sequelize?在续集的“where”子句中使用联结表中的列?
【发布时间】:2016-08-16 11:07:34
【问题描述】:

我想在 sequelize 模型中执行这个查询:

SELECT Cou.country_id,cou.country_name, Sta.state_id, Sta.state_name, Dis.district_id,
Dis.district_name,Cit.city_id, Cit.city_name,Loc.location_id,Loc.location_name,Sub_Loc.sub_location_id,Sub_Loc.sub_location_name,Prop.property_id,Prop.property_name,Prop.hp_builders_id,
Bud.builders_id,Bud.builders_name
FROM hp_country Cou
INNER JOIN hp_state Sta ON Cou.country_id = Sta.hp_country_id
INNER JOIN hp_district Dis ON Sta.state_id = Dis.hp_state_id
INNER JOIN hp_city Cit ON Dis.district_id = Cit.hp_district_id
INNER JOIN hp_location Loc ON Cit.city_id = Loc.hp_city_id
INNER JOIN hp_sub_location Sub_Loc ON Loc.location_id = Sub_Loc.hp_location_id
INNER JOIN hp_property Prop ON Sub_Loc.sub_location_id=Prop.hp_sub_location_id
LEFT JOIN hp_builders Bud ON Prop.hp_builders_id=Bud.builders_id
where (Cou.country_status=1 AND Sta.state_status=1 AND Cou.country_id=1)
AND (Dis.district_name LIKE '%ky%' OR Cit.city_name LIKE '%ky%' OR Loc.location_name LIKE '%ky%' OR Sub_Loc.sub_location_name LIKE '%ky%' OR Prop.property_name LIKE '%ky%')

我的 Sequelize 代码无法找到 hp_districts.district_status 列名。但是我得到了 hp_states.states_status 列。所以如何在 where 子句中获取这些剩余的表列名以及如何从 where 执行和/或子句条件

 hp_country.findAll({
            attributes: ['country_id', 'country_name'],
            where: {
                //main AND condition
                $and: [
                    //first joint condition
                    {
                        $and: [
                            { country_status: 1 },
                            { country_id: 1 },
          Sequelize.col("hp_states.state_status = 1")
                 ,
                Sequelize.col("hp_districts.district_status = 1")
                        ]

                    }
                ]
            },

            include: [
                {
                    model: hp_state,
                    attributes: ['state_id', 'state_name'],
                    required:true,
                    where:{
                        state_status:1
                    },
                    include:[{
                        model: hp_district,
                        attributes: ['district_id', 'district_name'],
                        where:{
                            district_status:1
                        },
                        required:true,
                        include:[{
                            model: hp_city,
                            where:{
                                city_status:1
                            }
                        }]
                    }]
                }
            ]
        })

【问题讨论】:

标签: javascript mysql node.js sequelize.js


【解决方案1】:

$ 符号将列名包装在main 中,允许对include 中的列进行查询

hp_country.findAll({
        attributes: ['country_id', 'country_name'],
        where: {
            country_status: 1,
            country_id: 1,
            '$States.state_status$': 1
        },
        include: [
            {
                model: hp_state,
                as: 'States',
                ...

【讨论】:

  • 是 $nested table.column name $ 支持 sequelize@v2.0.2 吗?我正在获取 hp_state 列。我的问题是访问 where 子句中的 hp_districts.district_status 列
猜你喜欢
  • 2014-07-02
  • 2018-12-13
  • 1970-01-01
  • 2017-02-17
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
相关资源
最近更新 更多