Efficiently inserting parent and children one-to-many
See original GitHub issueIssue type:
[x] question
Database system/driver:
[x] postgres
TypeORM version:
[x] latest
I have the following code that works as expected but I am unsure if this is the most efficient solution.
// Insert release
const release = Release.create(input);
await release.save();
// Assign each track the release ID
const tracks = input.tracks.map(track => ({
...track,
releaseId: release.id,
}));
// Bulk insert tracks
await getConnection()
.createQueryBuilder()
.insert()
.into(Track)
.values(tracks)
.execute();
I would like to be able to insert a parent (Release
) and its multiple children (Track
) in one operation. I’m not sure if I have missed something in the documentation, or if this is just not possible. Thanks
Release
entity:
@Entity('releases')
export class Release extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@OneToMany(() => Track, track => track.release)
tracks: Track[];
}
Track
entity:
@Entity('tracks')
export class Track extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ type: 'uuid', name: 'release_id' })
releaseId: string;
@ManyToOne(() => Release, release => release.tracks)
@JoinColumn({ name: 'release_id' })
release: Release;
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:17
- Comments:16 (1 by maintainers)
Top Results From Across the Web
TypeORM insert child OneToMany without loading the whole ...
I have a simple relation OneToMany parent - children. There are several operations that change the parent, and add a child.
Read more >The best way to map a @OneToMany relationship with JPA ...
This is the most natural way of mapping a database one-to-many database association, and, usually, the most efficient alternative too.
Read more >Most efficient way to map a @OneToMany relationship with ...
This is my first ever article. In this I will be covering all possible case in One-to-Many/Many-to-One entity association.
Read more >Chapter 23. Example: Parent/Child
We will explain how to use a bidirectional one-to-many association with cascades to model a parent/child relationship efficiently and elegantly.
Read more >Hibernate Tips: How to Prevent the Removal of a Parent Entity ...
Another option would be to rely on a database constraint. You then don't need to perform any validation in your Java application. This...
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 Free
Top 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
I have the same question! I thought that setting
{ cascade: true}
in the relation would do the trick but no, it tries to save the children without the parent id even if it already knows it. For now, I’m going to use the snippet shared here.I think you may need to add the
cascade: ['insert']
option to to your parent relation, like this@OneToMany(() => Track, track => track.release, { cascade: ['insert'] })
. That should allow you to save the release and automatically associate the child Tracks to it.