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

lazy loading, many-to-one, querying on the wrong entity id. #234

Closed
ericwooley opened this issue Jan 25, 2017 · 6 comments
Closed

lazy loading, many-to-one, querying on the wrong entity id. #234

ericwooley opened this issue Jan 25, 2017 · 6 comments
Labels

Comments

@ericwooley
Copy link

ericwooley commented Jan 25, 2017

I updated the details below after further investigation

I am having an issue where I am lazy loading a relationship, but a where an the wrong entity id is being queried

executing query: SELECT "lists"."id" AS "lists_id", "lists"."name" AS "lists_name", "lists"."owner" AS "lists_owner" FROM "shopping_list" "lists" INNER JOIN "user" "User" ON "User"."id"="lists"."owner" WHERE "lists"."id"=$1 -- PARAMETERS: [1]

Should just be
executing query: SELECT "lists"."id" AS "lists_id", "lists"."name" AS "lists_name", "lists"."owner" AS "lists_owner" FROM "shopping_list" "lists" INNER JOIN "user" "User" ON "User"."id"="lists"."owner" WHERE "user"."id"=$1 -- PARAMETERS: [1]

If I don't lazy load and use add the join to the repo.findById (everything works fine)[https://github.com/ericwooley/graphql-typeorm-passport-boilerplate/blob/master/src/graphql/queries/user.ts#L6]

const user = await userRepo.findOneById(request.user.id, {
      alias: 'user',
      innerJoinAndSelect: {
        "lists": "user.lists"
    }
})

I have a user class, with a lazy loaded one to many relationship:

@Table()
export default class User {
	// ...
	@OneToMany(type => ShoppingList, shoppingList => shoppingList.owner, {
		cascadeAll: true
	})
	lists: Promise<ShoppingList[]>
        // ... 
}

and a class for ShoppingList

@Table()
       // ...
	@ManyToOne(type => User, user => user.lists, {
		cascadeAll: true,
	})
	owner: Promise<User>
}

I cannot figure out for the life of me, why this where clause is being added. Maybe it's something dumb I am doing, but I have tried messing with it every way I can think of.

@pleerock
Copy link
Member

when you can user.list you want to load all lists of this SPECIFIC user. If your query will be:

SELECT "lists"."id" AS "lists_id", "lists"."name" AS "lists_name", "lists"."owner" AS "lists_owner" FROM "shopping_list" "lists" INNER JOIN "user" "User" ON "User"."id"="lists"."owner"

then it will load all lists of all users. That's why this id exist. Do you have problems with this query? Is result incorrect or something else?

@ericwooley
Copy link
Author

ericwooley commented Jan 25, 2017

@pleerock The problem is I am only getting one list because WHERE "lists"."id"=$1 -- PARAMETERS: [1] is being added. You are correct though, I mistakenly said it needs to have no where clause. What it needs is a where clause of WHERE "User"."id" = 1 as thats the id of the owner.

@Fokir
Copy link

Fokir commented Jan 25, 2017

It is not accidentally repeat my problem #191?

@ericwooley
Copy link
Author

@Fokir Thanks for pointing that out. I'll see if the alpha version fixes this later today.

@ericwooley
Copy link
Author

@Fokir

It does version 0.0.7-alpha.8 (or alpha.25, or 0.0.7, or 0.0.8) does not appear to fix my issue. I am going to update the issue now

@ericwooley ericwooley changed the title Extra where "id=1" clause is being added to lazy loading. lazy loading, many-to-one, querying on the wrong id. Jan 25, 2017
@ericwooley ericwooley changed the title lazy loading, many-to-one, querying on the wrong id. lazy loading, many-to-one, querying on the wrong entity id. Jan 25, 2017
@pleerock pleerock added the bug label Jan 26, 2017
@pleerock
Copy link
Member

fixed and released in 0.0.8-alpha.2

bbakhrom added a commit that referenced this issue Mar 19, 2019
…rectly from one-to-many and many-to-many sides"
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

3 participants