Cannot find how to query model where association is null in the docs
See original GitHub issueQuery for null association results
How to exclude the associations inside an include?
Hello everyone, I’ve been trying for really long to query for a model where its associations are null, but cannot find it in the docs o solve it anyway with the query. As I’m paginating the results, I’m offsetting and limiting the query result, so I really need to get the models where I have no associations in the form of an array.
Example snippet:
const words = await models.Word.findAll({
order: [['id', 'ASC']],
limit: 20,
offset,
include: [
{
model: models.Synonym,
attributes: ['id', 'name'],
},
],
// I've tried so many things here so I don't really know what else to try out
where: {
$Word.Synonyms$: null,
}
});
Using this query (and many other variations…) I tried to bring all the words where synonyms was null instead of an array, but couldn’t get to do that and had to do it with code after the query, but it breaks the pagination.
In my case the models are related this way:
models.Word.hasMany(models.Synonym);
models.Synonym.belongsTo(models.Word);
Thank you so much in advance, hope someone had solved this problem before. Would be great to have it in the docs with an example (I’m up to writing it and opening a PR if I get the problem solved).
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Thank you @andres112013 , I solved it by param
subQuery: false
Good day
Try to make your query like this
I hope it works for you