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

Wrong result when I'm using Where clause in include section #9010

Closed
H-Moradof opened this issue Feb 6, 2018 · 2 comments
Closed

Wrong result when I'm using Where clause in include section #9010

H-Moradof opened this issue Feb 6, 2018 · 2 comments

Comments

@H-Moradof
Copy link

H-Moradof commented Feb 6, 2018

What are you doing?

I have 3 model:
Page, PageShowPlace, ShowPlace

Actually 'PageShowPlace' is an junction table to make many to many relation between Page and ShowPlace.

Here is my association codes:

// page 1:m pageShowPlace m:1 showPlace
PageShowPlace.belongsTo(Page);
Page.hasMany(PageShowPlace);
PageShowPlace.belongsTo(ShowPlace);
ShowPlace.hasMany(PageShowPlace);

What do you expect to happen?

I wanted to get all pages of a specific showplace.

What is actually happening?

But the output includes all of my pages!
In some of output items pageShowPlaces is an empty (items that should NOT be in output) array and in the others "pageShowPlaces" is OK (correct items that must be in output)

[
    {
        "id": 1,
        "title": "Law",
        "pageShowPlaces": []
    },
    {
        "id": 2,
        "title": "Terms of services",
        "pageShowPlaces": []
    },
   {
        "id": 6,
        "title": "How to buy",
        "pageShowPlaces": [
            {
                "id": 8,
                "pageId": 6,
                "showPlaceId": 2,
                "showPlace": {
                    "id": 2,
                    "name": "Learning Box"
                }
            }
        ]
    }
]

Sequelize create query bellow:

SELECT "page"."id", "page"."title"
FROM "page" AS "page" 
LEFT OUTER JOIN 
    ( 
        "pageShowPlace" AS "pageShowPlaces" 
     	INNER JOIN "showPlace" AS "pageShowPlaces->showPlace" 
        	ON "pageShowPlaces"."showPlaceId" = "pageShowPlaces->showPlace"."id" 
        		AND "pageShowPlaces->showPlace"."id" = 2 
    ) 
    ON "page"."id" = "pageShowPlaces"."pageId";

BUT It should be something like this:

SELECT "page"."id", "page"."title"
FROM "page" AS "page" 
LEFT OUTER JOIN "pageShowPlace" AS "pageShowPlaces" 
	ON "page"."id" = "pageShowPlaces"."pageId" 
LEFT OUTER JOIN "showPlace" AS "pageShowPlaces->showPlace" 
	ON "pageShowPlaces"."showPlaceId" = "pageShowPlaces->showPlace"."id" 
WHERE "pageShowPlaces->showPlace"."id" = 2;

I expect to just get the last item in output, because the first two items are wrong

Sequelize version: ^4.32.2
Pg version: ^6.4.2
__pg-hstore version: ^2.3.2

note: I've search stackoverflow and sequelize documentation but nothing found,
I've also tried to put where in the root of query with many way but it has'nt worked...

@H-Moradof
Copy link
Author

H-Moradof commented Feb 7, 2018

I think the problem is that page should INNER JOIN to sub query but in LEFT OUTER JOIN with sub query.
I mean, it should be this

SELECT "page"."id", "page"."title"
FROM "page" AS "page" 
INNER JOIN 
    ( 
        ....
    ) 
    ON "page"."id" = "pageShowPlaces"."pageId";

instead of

SELECT "page"."id", "page"."title"
FROM "page" AS "page" 
LEFT OUTER JOIN
    ( 
        ....
    ) 
    ON "page"."id" = "pageShowPlaces"."pageId";

@H-Moradof
Copy link
Author

H-Moradof commented Feb 7, 2018

I've found the problem,
I must put Required:true into include section to sequelize use inner join instead of left outer join

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

No branches or pull requests

1 participant