Join relation with subquery
See original GitHub issueIssue 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:
- Created 5 years ago
- Comments:20 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Anything new ?
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!