question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Join relation with subquery

See original GitHub issue

Issue type:

[x] question

Database system/driver:

[x] postgres

TypeORM version:

[x] 0.2.0

Steps to reproduce or a small repository showing the problem:

I have the following relation Movie -> Actors:

import {
    Entity,
    Column,
    ManyToMany,
    JoinTable,
} from "typeorm"
import { Actor } from "@app/entity"

@Entity("movies")
export class Movie {
    @Column({ type: "text" })
    title: string

    @ManyToMany(type => Actor, actor => actor.movies)
    @JoinTable({
        name: "movies_actors",
        joinColumn: { name: "movieId" },
        inverseJoinColumn: { name: "actorId" }
    })
    actors: Actor[]
}

with query.leftJoinAndSelect("movie.actors", "actor") I get following query:

SELECT
  "movie"."id" AS "movie_id",
  "movie"."title" AS "movie_title",
  "actors"."id" AS "actors_id",
  "actors"."name" AS "actors_name"
FROM "movies" "movie"
LEFT JOIN "movies_actors" "movie_actors"
  ON "movie_actors"."movieId" = "movie"."id"
LEFT JOIN "actors" "actors"
  ON "actors"."id" = "movie_actors"."actorId"

which works fine. If I want to apply limit/offset to actors, I have to generate query like this:

SELECT
  "movie"."id" AS "movie_id",
  "movie"."title" AS "movie_title",
  "actors"."id" AS "actors_id",
  "actors"."name" AS "actors_name"
FROM "movies" "movie"
LEFT JOIN (
  SELECT * FROM (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY "movieId") AS "seqId"
    FROM "movies_actors"
  ) "movies_actors"
  WHERE "movies_actors"."seqId" BETWEEN 0 AND 5
) "movie_actors"
  ON "movie_actors"."movieId" = "movie"."id"
LEFT JOIN "actors" "actors"
  ON "actors"."id" = "movie_actors"."actorId"

I’m not sure how can I do this with query builder, if I use subquery in join, it seems I have to manually replicate what TypeORM does with respect to relationship type, join table column names, ON statements etc. in subquery. Is there any other way of doing it, where I have control over select statement inside join, but everything else stays the same?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

19reactions
GaryChangCNcommented, Aug 4, 2020

Anything new ?

9reactions
onderonurcommented, Feb 14, 2020

yeah that shall work however for map many “user.photos” is just a property name, it does not care if its a relation or something else. And btw most of times its really better to execute a separate queries and map the data in terms of both performance and code, so I was thinking to deprecate leftJoinAndMapMany method, maybe it will be in the future.

So you recommend to execute separate queries and I’ve read in another issue that you usually load your entities first and execute queries for things like relation counts etc and map that data to properties of your entities. Does this mean multiple db hits to collect the desired data? Or are you talking about creating subqueries and using addSelect etc?

It might be a really silly question. But many people I know just create giant queries with a lot of joins and execute them once. I can’t say that is always performant but when there are more than one db hits, many people goes crazy. I mostly work as a front-end developer and don’t have too solid knowledge about this kind of stuff. Can you clarify this a little bit more? (Maybe with some examples)

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use subquery in JOIN operation in MySQL
A subquery can be used with JOIN operation. In the example below, the subquery actually returns a temporary table which is handled by...
Read more >
Joins and Subqueries in SQL - Web Age Solutions
An SQL Join statement is used to combine data or rows from two or more tables based on a common field between them....
Read more >
Subquery vs. JOIN - LearnSQL.com
Subqueries and JOIN s can both be used in a complex query to select data from multiple tables, but they do so in...
Read more >
Subqueries and JOINs - MariaDB Knowledge Base
Rewriting Subqueries as JOINS. A subquery using IN can be rewritten with the DISTINCT keyword, for example: SELECT * FROM table1 WHERE col1...
Read more >
In SQL or MySQL, can we join a table and a subquery result?
yes, sql works on sets, a subquery returns a set as result, so this is possible. you have to give the subquery a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found