typeorm: Cascade insert one to many not working
Issue type:
[X] question [X] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[X] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[X] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Steps to reproduce or a small repository showing the problem:
import {Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn,CreateDateColumn, UpdateDateColumn, Timestamp, OneToMany} from "typeorm";
import {Profile} from "./profile";
import {Job} from "./job"
@Entity()
export class User {
@PrimaryGeneratedColumn("uuid")
public id: string;
@Column()
public name: string;
@OneToOne(type => Profile, {cascade: true})
@JoinColumn()
public profile: Profile;
@OneToMany(type => Job, job => job.user , {cascade: true})
public job: Job[];
@CreateDateColumn()
public CreatedAt: Timestamp;
@UpdateDateColumn()
public UpdatedAt: Timestamp;
}
import {Entity, PrimaryGeneratedColumn, Column,CreateDateColumn, UpdateDateColumn, Timestamp, ManyToOne, OneToMany} from "typeorm";
import { User } from "./user";
export enum JobStatus {
CLOSED = "closed",
OPEN = "open",
NEW = "new"
}
@Entity()
export class Job {
@PrimaryGeneratedColumn("uuid")
public id: string;
@Column()
public description: string;
@Column()
public State: JobStatus;
@ManyToOne(type => User, user => user.job,{onDelete:'CASCADE'})
public user: User;
@CreateDateColumn()
public CreatedAt: Timestamp;
@UpdateDateColumn()
public UpdatedAt: Timestamp;
}
When Create User with multiple jobs, only last job saved into the database.
user =
{
"name": "1",
"profile": {
"gender": "M",
"description": "This is Chain"
},
"job": [
{
"description": "NEW",
"State": "NEW"
},
{
"description": "OPEN",
"State": "OPEN"
}
]
}
const addUser = async (user: User) => {
const connection = getConnection();
return connection.manager.save(user)
}
and return value is
{
"name": "1",
"profile": {
"gender": "M",
"description": "This is Chain",
"id": "d09d76fd-1efc-4e3a-9aeb-f1b4eb5212d6",
"CreatedAt": "2019-03-20T09:11:18.720Z",
"UpdatedAt": "2019-03-20T09:11:18.720Z"
},
"job": [
{
"description": "OPEN",
"State": "OPEN",
"id": "0672b671-1d82-4ea1-89ec-ee3065b6529e",
"CreatedAt": "2019-03-20T09:11:18.720Z",
"UpdatedAt": "2019-03-20T09:11:18.720Z"
},
{
"description": "OPEN",
"State": "OPEN",
"id": "0672b671-1d82-4ea1-89ec-ee3065b6529e",
"CreatedAt": "2019-03-20T09:11:18.720Z",
"UpdatedAt": "2019-03-20T09:11:18.720Z"
}
],
"id": "3b81d407-b58f-4471-a59c-fb9e50504d80",
"CreatedAt": "2019-03-20T09:11:18.720Z",
"UpdatedAt": "2019-03-20T09:11:18.720Z"
}
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 34
- Comments: 15
before line “return connection.manager.save(user)” use the connection.manager.create(User,user)… and then save the returned object… this resolved for me 👍
I found that in order to cascade one to many, lazy load has to be set to false.
<dripping-sarcasm> Of course it does.. </dripping-sarcasm>I’m using the EntitySchema-in-a-separate-file approach, and there is a lazy property associated with the relations array elements.
It’s been a while, so I don’t remember the why. Sorry.