Load relation ids without joining target entity
See original GitHub issuetl;dr what’s the best way to fetch relation ids from a join table?
I have a data model that is unfortunately spread out across several databases, so I need to handle some relations manually with separate queries on other connections. What I’m trying to do is find a way to fetch relation ids from a join table only; typeorm adds an additional join to the related class’s table, which in my case, exists in another database.
// Database A
@Entity('usr')
class User {
@PrimaryGeneratedColumn()
public id: number;
@Column()
public name: string;
}
// Database B
@Entity()
class Article {
@PrimaryGeneratedColumn()
public id: number;
@ManyToMany(() => User)
@JoinTable({name: 'article_authorship', inverseJoinColumn: {name: 'author_id'}})
public authors: User[] = [];
public authorIds: number[] = [];
}
The article_authorship
join table is also on Database B and looks like:
Column | Type | Modifiers
------------+---------+-----------
article_id | bigint | not null
author_id | bigint | not null
ordinal | integer | not null
I’ve tried a few different things in my controller to populate Article#authors
or Article#authorIds
with data, or otherwise obtain an array of ids from that join table.
SelectQueryBuilder#innerJoinAndMapMany
const articles = articleRepository.createQueryBuilder('article');
articles.innerJoinAndMapMany('article.authorIds', 'article_authorship', 'authorship', 'article.id = authorship.article_id');
This almost works. The following query is executed:
SELECT "article"."id" AS "article_id", "article"."headline" AS "article_headline", "article"."content" AS "article_content", "authorship"."article_id" AS "authorship_article_id", "authorship"."author_id" AS "authorship_author_id" FROM "article" "article" INNER JOIN "article_authorship" "authorship" ON "article"."id" = authorship.article_id WHERE ("article"."date_deleted" IS NULL) AND "article"."id" IN ($1) -- PARAMETERS: ["41255"]
For an Article with 2 authors, this does in fact load 2 records with each User id; I can see that in the raw results from SelectQueryBuilder#getRawAndEntities
. However, in the single entity that’s returned, Article#authorIds
is still empty. The transformation from raw results to entities leaves that out.
SelectQueryBuilder#loadAllRelationIds
const articles = articleRepository.createQueryBuilder('article');
articles.loadAllRelationIds();
This results in the following query:
SELECT "article_authors_relation_id"."author_id" AS "author_id", "article_authors_relation_id"."article_id" AS "article_id" FROM "usr" "usr" INNER JOIN "article_authorship" "article_authors_relation_id" ON ("article_authors_relation_id"."article_id" = $1 AND "article_authors_relation_id"."author_id" = "usr"."id") ORDER BY "article_authors_relation_id"."author_id" ASC, "article_authors_relation_id"."article_id" ASC -- PARAMETERS: ["41255"]
Obviously, this fails outright, as the usr
table is on Database A. I also attempted to use SelectQueryBuilder#loadRelationIdAndMap)
and heavily modify the query but no luck there.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Do you simply want to load ids from junction (many-to-many) table, right?
See examples here.
Closing as no answer from reportee. Btw, cross-database requests are supported now. You can find docs here.