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.

How can I use a limit and a required in an include?

See original GitHub issue

Hi, I am trying to query an associated model via an include. I want to return only the models of the matched parents, returning only the first match of the children.

hook.params.sequelize = {
     include: [
          {
               model: 'executions',
               where: {
                    startedOn: null,
                    finishedOn: null,
               },
               limit: 1,
               required: true,
          }
     ]
}

Actual behavior

The result returns all the parents and it doesn’t matter if have executions or not.

 "data": [
    {
       id: 'dcc0aedf-c0d6-4bbe-b34d-257b8e8e8263',
       action: 9,
       executions: [ [executions] ]
    },
    {
       id: '31dadc19-1b8b-4b59-8ae6-ab9e28afcfb5',
       action: 13,
       executions: []
    }, 
   {
       id: '442a8921-11fa-4e9c-ad8e-fe57ea6cc854',
       action: 8,
       executions: [ [executions] ]
    }, 
 ]

Expected behavior

I want that the result returns only the parents that have executions.

 "data": [
    {
       id: 'dcc0aedf-c0d6-4bbe-b34d-257b8e8e8263',
       action: 9,
       executions: [ [executions] ]
    },
   {
       id: '442a8921-11fa-4e9c-ad8e-fe57ea6cc854',
       action: 8,
       executions: [ [executions] ]
    }, 
 ]

System configuration

“feathers-sequelize”: “^6.2.0”

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
MariaCamilaCubidescommented, Aug 25, 2021

Thank you so much, that works with a little change. I had to add the property duplicating: false because before that I had missing FROM-clause entry for table "executions" at find execution-groups error, so I searched and found a solution here https://github.com/feathersjs-ecosystem/feathers-sequelize/issues/248.

hook.params.query = {
    '$executions.startedOn$': null,
    '$executions.finishedOn$': null,
    $limit: 1,
  };
  const association = {
    include: [
      {
        model: hook.app.service('executions').Model,
        as: 'executions',
        duplicating: false,
      },
    ],
  };
  hook.params.sequelize = Object.assign(association, { raw: false });
0reactions
chrisbagcommented, Nov 17, 2021

For those ending up on this thread, duplicating false does solve the issue but messes up the limit and offset. For this to work you need to make separate queries (separate:true). This works for one-to-many relations but does not work with many-to-many as is indicated in this thread which is 7 years old and has still has not been solved https://github.com/sequelize/sequelize/pull/4525 and https://github.com/sequelize/sequelize/issues/8457

Read more comments on GitHub >

github_iconTop Results From Across the Web

how can i use limit in include model using sequelize
Show activity on this post. i want to get user's images at limit 2 from Follow model. so, i tried get user's images...
Read more >
Limits intro (article) | Khan Academy
Limits describe how a function behaves near a point, instead of at that point. This simple yet powerful idea is the basis of...
Read more >
What Is a Limit Order in Trading, and How Does It Work?
A limit order is used to buy or sell a security at a pre-determined price and will not execute unless the security's price...
Read more >
Learn How to Use LIMIT Clause in SQL - Simplilearn
The LIMIT clause states that only maximum rows are included in the result collection (or exactly the maximum rows, in the event that...
Read more >
LIMIT Clause - Apache Impala
This technique can be used to simulate "paged" results. Because Impala queries typically involve substantial amounts of I/O, use this technique only for ......
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