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

Filtering with nested include #11036

Open
magp3 opened this issue Jun 5, 2019 · 4 comments
Open

Filtering with nested include #11036

magp3 opened this issue Jun 5, 2019 · 4 comments
Labels
status: understood For issues. Applied when the issue is understood / reproducible. type: bug

Comments

@magp3
Copy link

magp3 commented Jun 5, 2019

What are you doing?

Trying to filter on an associated field.

const A = Connection.define('a');

const B = Connection.define('b');

A.belongsTo(B);
B.hasMany(A);

const C = Connection.define('c',
{
    myField:Sequelize.STRING
});


B.belongsTo(C);


const main = async()=>
{
    await Connection.sync({force:true});

    const c= await C.create({myField:"123"});
    const b = await B.create({cId:c.id});
    const a = await A.create({bId:b.id});

    const resA = await A.findAll({
        logging:console.log,
        include:[{model:B,include:[{model:C,where:{myField:"dontCare"}}]}]
    });
    const resB = await A.findAll({
        logging:console.log,
        include:[{model:B,where:{id:{[Op.gt]:0}},include:[{model:C,where:{myField:"dontCare"}}]}]
    });
    console.log(resA.map(a=>a.get({plain:true})));
    console.log(resB.map(a=>a.get({plain:true})));
};

main();

To Reproduce
Run the code above.

What do you expect to happen?

I'm not entirely sure what the correct way of doing this kind of filtering is. But
I did not expect these 2 queries to produce a different result like this.

What is actually happening?

The first query runs the nested where on a different level from the second query. Seems inconsistent.
The inner where clause is shifted out when I include the useless filtering
where:{id:{[Op.gt]:0}}

Output:

SELECT `a`.`id`, `a`.`createdAt`, `a`.`updatedAt`, `a`.`bId`, `b`.`id` AS `b.id`, `b`.`createdAt` AS 
`b.createdAt`, `b`.`updatedAt` AS `b.updatedAt`, `b`.`cId` AS `b.cId`, `b->c`.`id` AS
 `b.c.id`, `b->c`.`myField` AS `b.c.myField`, `b->c`.`createdAt` AS `b.c.createdAt`, `b->c`.`updatedAt` AS 
`b.c.updatedAt` FROM `as` AS `a` LEFT OUTER JOIN ( `bs` AS `b` INNER JOIN `cs` AS `b->c` ON
 `b`.`cId` = `b->c`.`id` AND `b->c`.`myField` = 'dontCare' ) ON `a`.`bId` = `b`.`id`;
SELECT `a`.`id`, `a`.`createdAt`, `a`.`updatedAt`, `a`.`bId`, `b`.`id` AS `b.id`, `b`.`createdAt` AS 
`b.createdAt`, `b`.`updatedAt` AS `b.updatedAt`, `b`.`cId` AS
 `b.cId`, `b->c`.`id` AS `b.c.id`, `b->c`.`myField` AS `b.c.myField`, `b->c`.`createdAt` AS
 `b.c.createdAt`, `b->c`.`updatedAt` AS `b.c.updatedAt` FROM `as` AS `a` INNER JOIN 
`bs` AS `b` ON `a`.`bId` = `b`.`id` AND `b`.`id` > 0 INNER JOIN `cs` AS `b->c` ON 
`b`.`cId` = `b->c`.`id` AND `b->c`.`myField` = 'dontCare';

Environment

Dialect:

  • [X ] mysql
    Dialect library version: 1.6.5
    Database version: XXX
    Sequelize version: 5.8.7
@papb
Copy link
Member

papb commented Jul 27, 2019

Can you please post the outputs of the following lines:

console.log(resA.map(a=>a.get({plain:true})));
console.log(resB.map(a=>a.get({plain:true})));

@papb papb added the status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action label Jul 27, 2019
@magp3
Copy link
Author

magp3 commented Jul 29, 2019

Thanks for the response papb!

Here is the output.

[ { id: 1,
    createdAt: 2019-07-29T08:35:45.000Z,
    updatedAt: 2019-07-29T08:35:45.000Z,
    bId: 1,
    b: null } ]
[]


@papb
Copy link
Member

papb commented Jul 29, 2019

I was able to reproduce. Thank you very much.

By the way, replacing "dontCare" by "123" oddly produces the same result!! (i.e. the bug does not happen)

@papb papb added status: understood For issues. Applied when the issue is understood / reproducible. type: bug and removed status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action labels Jul 29, 2019
@papb papb self-assigned this Jul 29, 2019
@github-actions
Copy link
Contributor

github-actions bot commented Nov 9, 2021

This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 9, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: understood For issues. Applied when the issue is understood / reproducible. type: bug
Projects
Development

No branches or pull requests

3 participants