描述与问题
租金是所有注册租金的表,开始日期和结束日期均为物业。在查询之后,我只想得到这个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不返回的租金相冲突)
在本例中,我希望只有ProductGroup与ID=1一起使用IDs=2产品,4
发布于 2019-05-21 14:08:51
尝试将where子句添加到产品级别:
where: {
'$rental_table_name.primary_key$' : { [Op.eq]: null }
},
(酌情替换表名和PK )。
更新解决方案:
请在“产品组”部分尝试以下操作:
where: {
'$products.rentals.ID$' : { [Op.eq]: null }
},
https://stackoverflow.com/questions/56229144
复制相似问题