Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findAll with include and multiple conditions throws "missing FROM-clause entry for table" error #3401

Closed
MtDalPizzol opened this issue Mar 24, 2015 · 12 comments
Labels

Comments

@MtDalPizzol
Copy link

Hi guys! I have one table which keeps track of some "Businesses" and another one wich says wich of this Businesses are "ClosedBusinesses". I need to get the businesses that are not closed. Here's my code:

    model.Business.findAll({
        where: Sequelize.and(
            { filler_level_id: lv }, 
            {"ClosedBusiness.business_id": null }),
        include: [
            {
                model: model.ClosedBusiness,
                attributes: ['business_id','status'],
                required: false
            }, {
               model: model.RealEstate,
               attributes: ['id', 'address']
            }
        ],
    }).then(function(business) {
        fn(null, business);
    });

I'm getting the following error:

SequelizeDatabaseError: missing FROM-clause entry for table "ClosedBusiness"
    at module.exports.Query.formatError (/home/ubuntu/workspace/app/node_modules/sequelize/lib/dialects/postgres/query.js:301:16)
    at null.<anonymous> (/home/ubuntu/workspace/app/node_modules/sequelize/lib/dialects/postgres/query.js:64:21)
    at emit (events.js:95:17)
    at Query.handleError (/home/ubuntu/workspace/app/node_modules/pg/lib/query.js:99:8)
    at null.<anonymous> (/home/ubuntu/workspace/app/node_modules/pg/lib/client.js:166:26)
    at emit (events.js:95:17)
    at Socket.<anonymous> (/home/ubuntu/workspace/app/node_modules/pg/lib/connection.js:109:12)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:765:14)
    at Socket.emit (events.js:92:17)

And here is the query I need:

SELECT * FROM business 
    LEFT JOIN closed_business ON (
        business.id = closed_business.business_id)
    WHERE closed_business.business_id IS NULL
    AND
        business.filler_level_id = 2
@mickhansen
Copy link
Contributor

What does the failing query look like?

@mickhansen
Copy link
Contributor

Combining regular where and include based where in top level where isn't supported at the moment i'm afraid, we're working on a solution.

@MtDalPizzol
Copy link
Author

Sequelize thows the error before it can even log the query. =/
Any other way I can work around this?

@mickhansen
Copy link
Contributor

include.where won't since you need a negative query.
You could probably try a raw where. .and({filter_level: ...}, ['RAW QUERY']})

@MtDalPizzol
Copy link
Author

Hmmm... I think I'll just create a "closed" attribute on the Business table. At least until the solution you guys are working on came out....

@jez
Copy link

jez commented Aug 14, 2015

Not sure if it'd be best to report here or open a new issue, but I just got this with a nested include combined with an ORDER BY

var includeParams;

includeParams = [
  {
    model: models.Spot,
    include: [
      {
        model: models.User,
        as: 'Holder'
      }
    ]
  }, {
    model: models.User,
    as: 'Owners'
  }
];

models.Queue.findAll({
  include: includeParams,
  order: 'Queue.displayName ASC'
}).then(function(queues) {
  return res.json(queues);
});

And I only get the error on PostgreSQL (SQLite doesn't complain, even though it looks like the query is the same).

@janmeier
Copy link
Member

@jez Please show the model definitions and associations as well

@jez
Copy link

jez commented Aug 15, 2015

I've put up my models in this gist.

@stale
Copy link

stale bot commented Jun 29, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

@stale stale bot closed this as completed Jul 7, 2017
@satnam-sandhu
Copy link

Still an issue :(

@renefournier
Copy link

Would be nice to fix. (Version three can do this, btw.) In the meantime, guess we're back to writing raw queries.

@aliciagyt
Copy link

Still an issue :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants