'How do I remove the whole many-to-many relation with custom property?

I am using an entity similar to one described here. When I delete values from post.postToCategories, TypeORM does not delete the whole row with record, but sets one of the sides of the relation to null – detaching the issue.

How do I delete the whole row?



Solution 1:[1]

Although it is too late, but I am keeping the solution here for someone else who encounters same issue with deleting connections for many-to-many relationships that include custom properties.

Considering there are 2 entities User and Post having their many-to-many connection entity with custom properties named UserPost:

const user_post_connections: UserPosts[] = await this.userPostRepository.find(
  {
    where: {
      user: user,
    },
  },
);
if (user_post_connections?.length) {
  await this.userPostRepository.remove(user_post_connections);
}

NOTE: This solution is strictly for the case mentioned in this question with this setup reference.

Solution 2:[2]

This should be fixable with the orphanedRowAction option. Assuming you have a PostConnections join table that joins a user and a post, you can add orphanedRowAction: 'delete' to each ManyToOne relation like so:

  @ManyToOne(
    () => User,
    (user) => user.postConnections,
    { orphanedRowAction: 'delete' },
  )
  user: User;

  @ManyToOne(
    () => Post,
    (post) => post.postConnections,
    { orphanedRowAction: 'delete' },
  )
  post: Post;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Pezholio