[英]sequalize Search in Association
using sequelize 3.30.1
I have two models Category and Product related through association . 我通过关联有两个模型类别和产品相关。
var Category = sequelize.define('Category',
{ name: DataTypes.STRING });
};
var Product = sequelize.define("Product", {
price:{type:DataTypes.INTEGER,UNSIGNED:true},
{
classMethods: {
associate: function() {
Product.belongsTo(Category, {
foreignKey: {
name: 'category',
allowNull: false
}
});
});
how can i search for product with Category.name
like %CatName%
? 如何使用
Category.name
搜索产品,例如%CatName%
?
Sorry, but just had to re-format your code: 抱歉,只需重新格式化您的代码:
var Category = sequelize.define('Category',
{
name: DataTypes.STRING
});
var Product = sequelize.define("Product",
{
price: {
type: DataTypes.INTEGER,
UNSIGNED:true
},
classMethods: {
associate: function() {
Product.belongsTo(Category, {foreignKey: {name: 'category', allowNull: false}});
}
}
});
Now that you have the two models and defined an association, you can use the findAll() method to query. 现在您已经拥有了两个模型并定义了一个关联,您可以使用findAll()方法进行查询。
var catName = 'book';
Product.findAll({
include : [
{
model: Category,
where: {
name: {
$like: '%' + catName + '%'
}
}
}
]
})
.then(function(
console.log(result);
});
See if this works. 看看这是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.