typeorm
typeorm copied to clipboard
RelationId on a ManyToMany always eager loads ids
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[];
}
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.
This happens on OneToMany as well
Any changes?
Same for @OneToMany
for postgres. When using repo.find({select: [...fields]})
, relation ids load even if they are explicitly omitted from fields
array.
Any update?
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)
any changes?
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.
Posting only so that people realize that this is still an acute problem
bump ^