typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

RelationId on a ManyToMany always eager loads ids

Open dantman opened this issue 5 years ago • 10 comments

Issue type:

[ ] question [x] bug report [x] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] 0.2.13

Current behaviour:

Using @ManyToMany and @JoinTable to make relations in TypeORM, the relations are not eager loaded. i.e. They are only loaded if you use { relations: ['recipients'] } in the query, or use { loadRelationIds: { relations: ['recipients'] } } to load the ids.

However if you add a @RelationId for that relation, TypeORM will always eager load those IDs using a separate SQL query. i.e. SELECT crid.conversationId AS conversationId, crid.accountId AS accountId FROM account acc INNER JOIN conversation_recipients_account crid ON (crid.conversationId = ? AND crid.accountId = acc.id) ORDER BY crid.accountId ASC, crid.conversationId.

Expected behaviour:

@RelationId should not eagerly load relations, the value of the decorated prop should only be loaded if you query with { loadRelationIds: { relations: ['recipients'] } } or { relations: ['recipients']}.

At the very least, there should be an option to declare the RelationId as optional so it's not eager loaded.

Steps to reproduce or a small repository showing the problem:

@Entity('account')
export class Account {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToMany(() => Conversation, Conversation => Conversation.recipients, {
    onDelete: 'CASCADE',
    onUpdate: 'RESTRICT',
  })
  conversations: Conversation[];
}

@Entity('conversation')
export class Conversation {
  @PrimaryGeneratedColumn()
  id: number;

  @RelationId('recipients')
  recipientIds: number[];

  @ManyToMany(() => Account, Account => Account.conversations, {
    onDelete: 'CASCADE',
    onUpdate: 'RESTRICT',
  })
  @JoinTable()
  recipients: Account[];
}

dantman avatar Mar 21 '19 03:03 dantman

Ticket still open. Someone found a workaround? I want my object contain the list of Ids getting returned by @RelationId only on demand and i have no clue how to get this behaviour.

realSchoki avatar Feb 18 '20 12:02 realSchoki

This happens on OneToMany as well

jonnysamps avatar Nov 25 '20 16:11 jonnysamps

Any changes?

Mati365 avatar Jan 17 '21 15:01 Mati365

Same for @OneToMany for postgres. When using repo.find({select: [...fields]}), relation ids load even if they are explicitly omitted from fields array.

K-Kielak avatar Aug 31 '21 14:08 K-Kielak

Any update?

christian-forgacs avatar Dec 01 '21 15:12 christian-forgacs

I'm also interested in this, in general the RelationId decorator seems under-documented.

Any suggestions for how to help moving this forward? (still a bit unclear to me if current behaviour is intended)

Abildtoft avatar Dec 15 '21 11:12 Abildtoft

any changes?

fpnuseis avatar Oct 24 '22 15:10 fpnuseis

Any word from the maintainers on this? It's been almost 3 years now and this is a real problem. It would be nice to have some feedback from you.

jkworth avatar Jan 05 '23 19:01 jkworth

Posting only so that people realize that this is still an acute problem

albertodiazdorado avatar Sep 03 '23 12:09 albertodiazdorado

bump ^

Alfagun74 avatar Apr 17 '24 06:04 Alfagun74