question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Efficiently inserting parent and children one-to-many

See original GitHub issue

Issue 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:open
  • Created 5 years ago
  • Reactions:17
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
LordShiroecommented, May 30, 2019

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.

2reactions
jtownsleycommented, Oct 28, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found