【问题标题】:How to retrieved parent object which haven't children with specific values? Sequelize如何检索没有具有特定值的子对象的父对象?续集
【发布时间】:2019-10-07 07:24:02
【问题描述】:

说明和问题

Rentals 是包含所有已注册租赁的表格,其中包含开始和结束日期作为属性。查询后,我只想获取此 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 和 IDs=[2, 4] 的产品

【问题讨论】:

  • 您好,请在当前查询中添加一些示例数据、所需输出和获得的输出,请作为文本而不是图像
  • 我提供了该数据。

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


【解决方案1】:

尝试将 where 子句添加到您的产品级别:

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

(根据需要替换表名和 PK)。

更新的解决方案:

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

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

【讨论】:

  • 我收到“'on Clause' 中的未知列 'rentals.ID'”再次查看我的帖子 - 我提供了更多数据
  • 我的错误 - 我的建议是单个包含...而您有 两个。在产品组部分尝试以下操作:where: { '$product.rentals.ID$' : { [Op.eq]: null } }, 不确定这将在 sequelize 文档中的哪个位置...
  • @KenOn10 $products.rentals.ID$ 和 products.rentals.ID 有什么区别...???
  • 老实说,我不确定也找不到相关文档...但我认为美元符号通知 sequelize 该字段来自包含的模型。跨度>
  • @KenOn10 这是链接:sequelize.org/master/manual/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多