I have three tables user, userCareerProfile, userPersonalProfile with @OneToOne relationship.
User Entity:
@Entity({ name: 'users' })
export class User extends AbstractEntity {
@Column({ unique: true })
email: string;
@Column({ type: 'varchar', length: 50 })
full_name: string;
@OneToOne(() => UserPersonalProfile, (details) => details.user)
personal_details: UserPersonalProfile;
@OneToOne(() => UserCareerProfile, (career) => career.user)
career_profile: UserCareerProfile;
}
Personal Profile Entity:
@Entity()
export class UserPersonalProfile extends AbstractEntity {
@Column({ type: 'varchar', length: 20, nullable: true })
date_of_birth: string;
@Column({ type: 'varchar', length: 200, nullable: true })
address: string;
.....
@OneToOne(() => User, (user) => user.personal_details, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'user_id' })
user: User;
}
Career Profile Entity:
@Entity()
export class UserCareerProfile extends AbstractEntity {
@Column({ type: 'varchar', length: 100, nullable: true })
job_role: string;
@Column({ type: 'varchar', length: 100, nullable: true })
work_location: string;
.....
@OneToOne(() => User, (user) => user.career_profile, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'user_id' })
user: User;
}
Problem: When I am creating a new user, a new row is been adding to the user table but there is no record inserted in the other two tables. How can I be able to insert rows in the two tables based on the newly created user?
User Service:
public async createUser(userAttrs: Partial<User>): Promise<User> {
const user = await this._usersRepository.save(
this._usersRepository.create({
...userAttrs,
}),
);
return user;
}

