5

I have ManyToMany relationship of users and projects as follow:

// project.entity.ts

@Entity()
export class Project extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @OneToMany(type => ProjectUser, projectUser => projectUser.project)
  projectUsers: ProjectUser[];

  @CreateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', readonly: true })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  updatedAt: Date;

  @DeleteDateColumn({ type: 'timestamp' })
  deletedAt: Date;

}
// user.entity.ts

@Entity()
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  email: string;

  @Column()
  password: string;

  @OneToMany(type => ProjectUser, projectUser => projectUser.user)
  projectUsers: ProjectUser;

  @CreateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', readonly: true })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  updatedAt: Date;

  @DeleteDateColumn({ type: 'timestamp' })
  deletedAt: Date;
}
// project_user.entity.ts

@Entity()
export class ProjectUser extends BaseEntity{
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    projectId: number;

    @Column()
    userId: number;

    @Column()
    isUserCreator: boolean;

    @ManyToOne(type => Project, project => project.projectUsers)
    @JoinColumn()
    project: Project;

    @ManyToOne(type => User, user => user.projectUsers)
    @JoinColumn()
    user: User;
}

When I try to save a new project the new ProjectUser record does not save.

// project.service.ts


   async create(project: DeepPartial<Project>): Promise<Project> {
        ---> project  { name: 'project1', projectUsers: [{userId: 1, isUserCreator: true}]

        const projectToSave = Project.create(project)
        projectToSave.projectUsers = ProjectUser.create(project.projectUsers)
        return await projectToSave.save();
    }

Any ideas why cant I save the record in the related table?

1 Answer 1

3

You only created projectUsers instances but not saving them.

Use below code for saving all of the project and projectUsers:

const projectToSave = Project.create(project);
await projectToSave.save();

const projectUsers = ProjectUser.create(project.projectUsers);
projectUsers.forEach(p => p.project = projectToSave);
await projectUsers.save(projectUsers);
return projectToSave;

Or

const projectUsers = await ProjectUser.save(ProjectUser.create(project.projectUsers));
const projectToSave = Project.create(project);
projectToSave.projectUsers = projectUsers;
return await projectToSave.save();

Another solution You can just set cascade option in your entity to save automatically.

Sign up to request clarification or add additional context in comments.

4 Comments

Execute save the way I did would work for one to many relation, we can call it - deep save. The power of it is that the orm will set the id created for the new project record to the projectUser new records as projectId. I know I can break it down to separate methods but I thought typeOrm have a solution for that. The second example you wrote will cause runtime error since you can't save projectUser array before the project creation.
to make it possible you have to set cascade property as true in your entity
I thought cascade is relevant only for mutations after creation, as update/delete... I'll try that.
cascade works on all operations: cascade: ['insert', 'recover', 'remove', 'update', 'soft-remove']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.