简体   繁体   English

使用 OR 将多对多搜索序列化

[英]Sequelize Many to Many search using OR

The main idea for my code is to create a search field which I have two many to many fields, and I want to bind the search for the many to many table我的代码的主要思想是创建一个搜索字段,我有两个多对多字段,我想绑定对多对多表的搜索

I want to select the modelos.nome_modelo_produto field that is inside the table modelos, but never works, because the sequelize dont recognizes the field, even after I mentioned the relationship between the tables:我想选择表modelos内的modelos.nome_modelo_produto字段,但永远不会工作,因为sequelize无法识别该字段,即使我提到了表之间的关系:

Modelo.belongsToMany(Produto, {through: 'ProdutosModelos'});
Produto.belongsToMany(Modelo, {through: 'ProdutosModelos'});

And the idea for the search is this...搜索的想法是这样的......

Table.findAll({
where: {
            [Db.Sequelize.Op.or]: [
                {'nome_produto': Db.sequelize.where(Db.sequelize.fn('LOWER', Db.sequelize.col('nome_produto')), 'LIKE', '%' + req.query.search + '%')},

{'$modelos.nome_modelo_produto$': Db.sequelize.where(Db.sequelize.fn('LOWER',Db.sequelize.col('$modelos.nome_modelo_produto$')), 'LIKE', '%' + req.query.search + '%')}
            ]
}
})

But don't works... Can someone help-me or give-me a way to proceed ?但不起作用......有人可以帮助我或给我一种继续的方法吗?

If anyone have this error, I've checked that when you're using limit or offset tags inside your where there's a annoying error that I could'n solve.如果有人有这个错误,我已经检查过当你在你的内部使用限制或偏移标签时,有一个我无法解决的烦人的错误。

So the tip that I have is to remove the offset and limit, and also rename your table columns to find only by the name, for example, if you have the field tags in the table things, you can rename the tags field to things_tags, if it is a relational field, and when you set the field inside where you can only insert the field name without the table name.所以我的提示是删除偏移量和限制,并重命名表列以仅按名称查找,例如,如果表事物中有字段标签,则可以将标签字段重命名为things_tags,如果它是一个关系字段,并且当您在其中设置字段时,您只能插入字段名称而不插入表名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM