【发布时间】: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