Pra*_*ani 5 javascript node.js sequelize.js
我有两个表:tbl_product,tbl_product_category
我想制作一个带有关键字并返回与关键字匹配的所有产品和产品类别的API。如果结果来自,tbl_product则它也返回它是乘积。如果结果来自,tbl_product_category则它也返回它是类别。
tbl_product_catagory.findAll({
raw: true,
attributes: [[sequelize.literal("catagory_name"), "type"]],
include: [{
model: tbl_product,
attributes: [[sequelize.literal(["product_name"]), "name"]],
required: false,
where: {
product_name: {
[Op.like]: "%" + data.word + "%"
}
}
}]
})
Run Code Online (Sandbox Code Playgroud)
小智 6
我知道这已经很老了,但我喜欢使用 Nodejs 来解决这个问题,因为sequelize还没有联合支持。很简单,但有时我们没有意识到这一点:
例子:
Promise.all([
Blog.findAll({ include: { model: Author, required: true } }),
Blog.findAll({ include: { model: AnonymInfo, required: true } }),
]).then((modelReturn) => resolve(modelReturn.flat()))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1741 次 |
| 最近记录: |