Repository .create() with nested JSON, only one child goes through
See original GitHub issueIssue 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:
- Created 5 years ago
- Reactions:2
- Comments:14 (5 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
I’ll need to research this question. Not sure about milestone yet
it’s been two years, any updates?