sequelize / sequelize/sequelize

findAndCountAll with two or more included models.

Open
#14,465 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

Issue Creation Checklist

[+] I have read the contribution guidelines

Issue Description

I can't find the right way how to get all Posts with two or more related models. For example
I have a Posts with two associated model Author and Editor. So I need to find all posts where the Author.name or Editor.name is John Doe

I do next:

const {Sequelize, Op} = require('sequelize');
...
Posts.findAndCountAll({
      offset,
      limit: qty,
      include: [
           {
                model: Author,
                as: 'author',
                attributes: [ 'id', 'firstName', 'lastName', 'email' ],
                where: Sequelize.where(
                  Sequelize.fn("concat", Sequelize.fn('lower', Sequelize.col("author.firstName")), ' ', Sequelize.fn('lower', Sequelize.col("author.lastName"))),
                  {
                    [Op.like]: `%${search.toLowerCase()}%`,
                  },
                );
                or: true,
            },
            {
                model: Editor,
                as: 'editor',
                attributes: [ 'id', 'firstName', 'lastName', 'email' ],
                where: Sequelize.where(
                  Sequelize.fn("concat", Sequelize.fn('lower', Sequelize.col("editor.firstName")), ' ', Sequelize.fn('lower', Sequelize.col("editor.lastName"))),
                  {
                    [Op.like]: `%${search.toLowerCase()}%`,
                  },
                );
                or: true,
            },
      ],
});
What was unclear/insufficient/not covered in the documentation

According to this
https://github.com/sequelize/sequelize/blob/3e5b8772ef75169685fc96024366bca9958fee63/lib/model.js#L1302
I assume that [options.include[].or=false] should allow me to get all the Posts where the author or editor's name equals the searched name.

Can you help me to understand (with example if possible) how to do that in the right way?
The current result gives me the wrong data.

Thanks.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Posts.findAndCountAll query with the Author and Editor includes, then inspect the linked lib/model.js section for include[].or behavior. Compare the returned posts and count with the expected records where either associated name matches, and document a confirmed reproduction or the required correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, sql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.