gpt4 book ai didi

mysql - 如何检索没有具有特定值的子对象的父对象? Sequelize

转载 作者:搜寻专家 更新时间:2023-10-30 23:26:42 26 4
gpt4 key购买 nike

描述和问题

Rentals 是一个表格,其中包含所有已注册的租金,并将开始和结束日期作为属性。查询后,我只想获取此 productGroups,其中包含一些与给定时间段(“已过滤”obj)没有租金冲突的产品。

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

有人知道怎么弄吗?

信息

ProductGroup有很多Product

产品有很多出租

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 有一些可用的产品,产品的可用性取决于租金(如果产品的租金与提供的“过滤”对象不一致,则不要退还它们)

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

最佳答案

尝试在您的产品级别添加一个 where 子句:

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

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

更新的解决方案:

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

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

关于mysql - 如何检索没有具有特定值的子对象的父对象? Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56229144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com