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.

"exclude" instead of "include" / find where not exists

See original GitHub issue

The following:

    User.findAndCountAll({
      include: [
         { model: Profile, required: true}
      ]
    });

Will return users that have a profile.

How do I return users that have no profile? Is there an exclude?

Also, can I perform additional where statements on the relation? E.g. I want to return all users that have no profile with an avatar. Something like this?

    User.findAndCountAll({
      include: [
         { model: Profile, required: true, where: {avatar: null}}
      ]
    });

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:18
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
atomdmaccommented, Jul 19, 2017

I believe that this will fail with the following error if you include a “limit” clause.

Unhandled rejection SequelizeBaseError: missing FROM-clause entry for table "profiles"
2reactions
janmeiercommented, Jul 13, 2015

This can be achieved by adding userId IS NULL to the outer where clause. For it to be fully supported in sequelize you’ll need to wait for https://github.com/sequelize/sequelize/issues/3095, but in the meantime you can do

User.findAll({
    where: sequelize.literal('profiles.userId IS null'),
    include: [
    { model: Profile}
    ]
  })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Not exists - Exclude records those are having status in 0 ...
I want to get only B, C, D as output from the query. Lets say, 0 is status-complete & I want to ignore...
Read more >
Consider using [NOT] EXISTS instead of [NOT] IN with a ...
Phil Factor explains why you should prefer use of [NOT] EXISTS over [NOT] IN, when comparing data sets using a subquery.
Read more >
How to filter entities not contained from an existing list with ...
The WhereBulkNotContains method lets you filter a LINQ query by excluding all items from an existing list. Example. var customers = context.Customers.
Read more >
SQL Query to Exclude Records if it Matches an Entry in ...
NOT IN operators acts as a negation of In operator and return the results excluding the items present in the specified list. Syntax:...
Read more >
Syntax and Examples of SQL EXCLUDE - eduCBA
columns or field names that have to be fetched for the final result set. ... Find details of all the students excluding students...
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