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

Lateral join support in QueryBuilder #624

Closed
alexojegu opened this issue Jun 24, 2020 · 8 comments
Closed

Lateral join support in QueryBuilder #624

alexojegu opened this issue Jun 24, 2020 · 8 comments
Labels
enhancement New feature or request
Milestone

Comments

@alexojegu
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Currently only is possible use lateral join with raw queries:

SELECT e0.id, e1.* FROM gallery AS e0 LEFT JOIN LATERAL (
    SELECT e2.* FROM photo AS e2 WHERE e2.gallery_id = e0.id LIMIT ?
) AS e1 ON e0.id = e1.gallery_id WHERE gallery_id IN (?)

Describe the solution you'd like
Would be possible that QueryBuilder have support for lateral join.

Describe alternatives you've considered
Continue using raw queries.

Additional context
lateral join it is supported by MySQL and PostgreSQL, I am not sure about SQLite support.

@alexojegu alexojegu added the enhancement New feature or request label Jun 24, 2020
@B4nan
Copy link
Member

B4nan commented Jun 24, 2020

Knex does not support those, so not sure how easy it would be to integrate it. Here is the issue on their end (4 years old, no response :/ ):

knex/knex#1651

@B4nan
Copy link
Member

B4nan commented Jun 24, 2020

This issue is more up to date: knex/knex#3732

@alexojegu
Copy link
Contributor Author

alexojegu commented Jun 24, 2020

For now, to add more where clauses dynamically, I am trying:

    public async findGalleries(galleries: number[], limit: number): Promise<PhotoEntity[]> {
        const subquery = this.photoRepository.createQueryBuilder("e2").select("*");

        subquery.where({ gallery: { $eq: "e0.id" } });

        const results = await this.sqlConnection.execute(
            `select e0.id, e1.* from gallery as e0 left join lateral (
                ${subquery.orderBy({ created: "desc", id: "desc" }).limit(limit).getQuery()}
            ) as e1 on e1.gallery_id = e0.id where gallery_id in (?)`,
            [galleries],
        );

        /*
        const results = await this.sqlConnection.execute(
            `select e0.id, e1.* from gallery as e0 left join lateral (
                select e2.* from photo as e2 where e2.gallery_id = e0.id order by e2.created desc limit ?
            ) as e1 on e1.gallery_id = e0.id where gallery_id in (?)`,
            [limit, galleries],
        );
        */

        return results.map((result) => this.photoRepository.map(result));
    }

But I can't get it work.

Update: Using knex.raw works.

@SacDin
Copy link

SacDin commented Jan 5, 2021

This is indeed important feature, should be prioritised.

@B4nan
Copy link
Member

B4nan commented Jan 5, 2021

This is blocked by knex not supporting it, if you want to see this feature in MikroORM, better to ask there first.

@robbyemmert
Copy link

Knex does support raw query fragments. Is there a way to do this with raw query fragments?

@B4nan
Copy link
Member

B4nan commented Sep 6, 2022

Not really, but you can build any query (let it be via knex or any other QB or even manually), and execute and map it to the entities.

https://mikro-orm.io/docs/query-builder#mapping-raw-results-to-entities

@B4nan B4nan added this to the 6.0 milestone Oct 31, 2023
@B4nan
Copy link
Member

B4nan commented Oct 31, 2023

Implemented in v6 via 5c5174e

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

No branches or pull requests

4 participants