-
-
Notifications
You must be signed in to change notification settings - Fork 496
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
QueryBuilder join fails with "Metadata for entity undefined not found" #4353
Comments
Not sure what you mean by that, you shouldn't be installing |
@B4nan thanks, removing Just for the record, this seems to be the line referenced in that |
Oh right, that's indeed wrong, thanks for the hint. |
Hmm, this is not really a bug, I will add a validation for this - the problem is your QB instance uses default alias, which is Both of this will work: const qb = orm.em.createQueryBuilder(Offer);
const offers = await qb.join('o0.campaigns', 'c').select(['*']).getResultList();
const qb = orm.em.createQueryBuilder(Offer, 'o');
const offers = await qb.join('o.campaigns', 'c').select(['*']).getResultList(); Or just skip the alias, you dont need to provide that for root entity: const qb = orm.em.createQueryBuilder(Offer);
const offers = await qb.join('campaigns', 'c').select(['*']).getResultList(); |
@B4nan Confirmed all 3 solutions, thank you. Really appreciate your help. |
@B4nan would you please take a look, should I start a separate thread for this? const qb = em.createQueryBuilder(Offer, 'o');
const offers = await qb.join('o.campaigns', 'c')
.select([
'*',
qb.raw('COUNT(DISTINCT c.id) AS campaigns_number')
])
.where(filter)
.groupBy('o.id')
.having('campaigns_number > 1')
.offset(0)
.limit(10)
.getResultList(); The above crashes with:
The generated query indeed fails to execute in mysql console. SELECT
`o`.*,
COUNT(DISTINCT c.id) AS campaigns_number
FROM
`offer` AS `o`
INNER JOIN `campaign` AS `c` ON `o`.`id` = `c`.`offer_id`
WHERE
`o`.`id` in(
SELECT
`o`.`id` FROM (
SELECT
`o`.`id` FROM `offer` AS `o`
INNER JOIN `campaign` AS `c` ON `o`.`id` = `c`.`offer_id`
GROUP BY
`o`.`id`
HAVING (campaigns_number > 1)
LIMIT 10) AS `o`)
GROUP BY
`o`.`id`
HAVING (campaigns_number > 1)
Below is the reference query that I'm trying to translate: SELECT
`o`.*,
COUNT(DISTINCT c.id) AS campaigns_number
FROM
`offer` AS `o`
INNER JOIN `campaign` AS `c` ON `o`.`id` = `c`.`offer_id`
GROUP BY
`o`.`id`
HAVING (campaigns_number > 1) |
Disable pagination via |
I guess it should be allowed only when you don't provide the grouping clause. |
@B4nan Works perfectly now. Thank you! |
Trying to use join on two entities with QueryBuilder makes my app crash.
Would appreciate any help trying to solve this.
Stack trace
To Reproduce
Expected behavior
Query does not fail and actually returns results.
Additional context
I also could not update to mikro-orm@5.7 since it requests an old version ofmysql2
as a peer dependency.Versions
The text was updated successfully, but these errors were encountered: