question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot find how to query model where association is null in the docs

See original GitHub issue

Query 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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
luqimincommented, Feb 12, 2020

Thank you @andres112013 , I solved it by param subQuery: false

4reactions
EduardMcflycommented, Jan 27, 2020

Good day

Try to make your query like this

import { Op } from "sequelize";
const words = await models.Word.findAll({
  order: [["id", "ASC"]],
  limit: 20,
  offset,
  include: [
    {
      model: models.Synonym,
      attributes: ["id", "name"]
    }
  ],
  //  When you need to condition the associations, use Sequelize.where
  where: Sequelize.where(Sequelize.col(`word.synonymId`), Op.is, null)
});

I hope it works for you

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tip: Find records missing an association with `where.missing`
When it comes to raw SQL, you can use a LEFT OUTER JOIN combined with a NULL check to find records without certain...
Read more >
Want to find records with no associated records in Rails
Consider a simple association...
Read more >
Eager Loading - Sequelize
In Sequelize, eager loading is mainly done by using the include option on a model finder query (such as findOne , findAll ,...
Read more >
Active Record Query Interface - Ruby on Rails Guides
Null Relation ; Readonly Objects; Locking Records for Update ... The primary operation of Model.find(options) can be summarized as:.
Read more >
Model field reference - Django documentation
Avoid using null on string-based fields such as CharField and TextField . ... clean() on the model in order to programmatically supply any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found