Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.5K
Views
TypeORM: Save entity with ManyToMany relation with custom field does not save properly

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!