如何检索没有特定值的子对象的父对象?续集

时间:2019-05-20 22:55:52

标签: mysql sql node.js database sequelize.js

说明和问题

租金是具有所有已注册租金的表格,其中开始日期和结束日期为属性。查询后,我只想获得此productGroups,其中的某些产品在给定的时间段内(“已过滤” obj)没有租金冲突。

我认为我只需要添加(以某种方式)具有空的“出租”属性的条件。与产品属性相同。

有人知道如何得到这个吗?

信息

ProductGroup有很多产品

产品有很多租赁

filtered.startDate,filtered.endDate是新租期的开始和结束日期

ProductGroup.findAll({
        include: [
            {
                model: Product,
                include: [
                    {
                        model: Rentals,
                        where: {
                            [Op.or]: [
                                {
                                    startDate: {
                                        [Op.between]: [filtered.startDate, filtered.endDate]
                                    }
                                },
                                {
                                    endDate: {
                                        [Op.between]: [filtered.startDate, filtered.endDate]
                                    }
                                }]
                        },
                        required: false
                    }
                ],
                required: true
            }
        ]
    })

更新:

型号:


module.exports = function(sequelize, DataTypes) {
    let productGroups = sequelize.define('productGroups', {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
            field: 'ID'
        },
        name: {
            type: DataTypes.STRING(255),
            allowNull: false,
            unique: true,
            field: 'name'
        }
    }, {
        timestamps: false,
        tableName: 'product_groups'
    });

    productGroups.associate = ( models ) =>{
        productGroups.hasMany( models.product, { foreignKey: 'productGroupId', sourceKey: 'id' } );
    };

    return productGroups;
};

module.exports = function(sequelize, DataTypes) {
    let product = sequelize.define('product', {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
            field: 'ID'
        },
        name: {
            type: DataTypes.STRING(255),
            allowNull: false,
            field: 'name'
        },
        productGroupId: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            references: {
                model: 'product_groups',
                key: 'ID'
            },
            field: 'product_group'
        }
    }, {
        timestamps: false,
        tableName: 'product'
    });

    product.associate = ( models ) =>{
        product.belongsTo( models.productGroups, { foreignKey: 'productGroupId', targetKey: 'id' } );

        product.hasMany( models.rentals, { foreignKey: 'productId', sourceKey: 'id' } );
    };

    return product;
};

module.exports = function(sequelize, DataTypes) {
    let rentals = sequelize.define('rentals', {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
            field: 'ID'
        },
        startDate: {
            type: DataTypes.DATEONLY,
            allowNull: false,
            field: 'start_date'
        },
        endDate: {
            type: DataTypes.DATEONLY,
            allowNull: false,
            field: 'end_date'
        },
        productId: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            references: {
                model: 'product',
                key: 'ID'
            },
            field: 'product_id'
        }
    }, {
        timestamps: false,
        tableName: 'rentals'
    });

    rentals.associate = ( models ) =>{
        rentals.belongsTo( models.product, { foreignKey: 'productId', targetKey: 'id' } );
    };

    return rentals;
};

样本过滤参数:

filtered = {
    startDate: 2019-05-20,
    endDate: 2019-05-29,
} //dates are date objects

样本表:

product_groups = [
    {ID: 1, name: 'test1'},
    {ID: 2, name: 'test2'}
]

product = [
    {ID: 1, name: 'prod1', product_group: 1},
    {ID: 2, name: 'prod2', product_group: 1},
    {ID: 3, name: 'prod3', product_group: 2},
    {ID: 4, name: 'prod4', product_group: 1},
]

rentals = [
    {ID: 1, start_date: 2017-10-01, end_date: 2017-10-25, product_id: 1},
    {ID: 2, start_date: 2019-05-27, end_date: 2019-05-31, product_id: 1},
    {ID: 3, start_date: 2018-10-12, end_date: 2018-10-14, product_id: 2},
    {ID: 4, start_date: 2019-05-10, end_date: 2019-06-31, product_id: 3}
] //dates are date objects

因此,我希望只有具有某些可用产品的productGroups, 产品的可用性取决于租金(如果产品的租金与提供的“过滤的” obj相冲突,则不退还)

在此示例中,我只希望ID = 1的ProductGroup和ID = [2、4]的产品

1 个答案:

答案 0 :(得分:0)

尝试在产品级别添加where子句:

        where: { 
            '$rental_table_name.primary_key$' : { [Op.eq]: null }
        },

(适当替换表名和PK)。

更新的解决方案:

在“产品组”部分尝试以下操作:

   where: { 
       '$products.rentals.ID$' : { [Op.eq]: null } 
   },