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.

Repository .create() with nested JSON, only one child goes through

See original GitHub issue

Issue type:

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

TypeORM version:

[ ] latest [x] @next [x] 0.2.0-alpha.46

Steps to reproduce or a small repository showing the problem:

When trying to INSERT object with children via repository .create() then .save(), only the latest child is being inserted. The rest of the children get lost during .create().

Endpoint callback:

async create(role: CreateRoleWithLevelsDto): Promise<Role> {
    const newRole = this.roleRepository.create(role);
    return await this.roleRepository.save(newRole);
}

Entities:

@Entity()
export class Role {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @OneToMany(type => RoleLevel, roleLevel => roleLevel.role, { cascade: true, eager: true })
  roleLevels: RoleLevel[];
}

@Entity()
export class RoleLevel {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  levelId: number;

  @Column({ nullable: true })
  gradingId: number;

  @Column({ nullable: true })
  name: string;

  @Column({ nullable: false })
  roleId: number;

  @ManyToOne(type => Role, role => role.roleLevels, { nullable: false, onDelete: 'CASCADE' })
  role: Role;
}

Data sent from frontend:

{ id: null,
  name: 'UI Designer',
  buildingBlock: null,
  jobCluster: null,
  roleLevels:
   [ { id: null,
       roleId: null,
       levelId: 2,
       gradingId: 3,
       name: 'Junior' },
     { id: null,
       roleId: null,
       levelId: 3,
       gradingId: 4,
       name: null },
     { id: null,
       roleId: null,
       levelId: 4,
       gradingId: 5,
       name: 'Senior' } ] }

What comes out of create, i.e. what is being saved into the database:

Role {
  id: null,
  name: 'UI Designer',
  buildingBlock: null,
  jobCluster: null,
  roleLevels:
   [ RoleLevel {
       id: null,
       levelId: 4,
       gradingId: 5,
       name: 'Senior',
       roleId: null } ] }

Many thanks for the great work and effort!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
pleerockcommented, Aug 4, 2018

I’ll need to research this question. Not sure about milestone yet

1reaction
abbbel2commented, Feb 3, 2021

it’s been two years, any updates?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make nested JSON response from a single table
How to make nested JSON response from a single table ; public ResponseEntity<Map<String, Object>> retrieveData ; () { Iterable<CountryModel> stud ...
Read more >
REST API Design Best Practices for Sub and Nested Resources
In general, using nested resources isn't as flexible as using root resources only. For example, if we have a many-to-many relationship.
Read more >
How to Deserialize a Complex JSON Object in C# .NET
In this article, we are gonig to learn how to deserialize a complex JSON object using C# as our language of choice.
Read more >
26. Learn to Write Json Path for Simple and Nested ... - YouTube
In this video, I have tried to explain writing Json Path for simple and nested JSON Objects. A nested JSON object is what...
Read more >
nested child elements from json in javascript - You.com
If you are passing JSON like objects inside object, then it can have JSON like below: In the above JSON, we have nested...
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