From Repo:
remove- Removes a given entity or array of entities. It removes all given entities in a single transaction (in the case of entity, manager is not transactional).
Example:
await repository.remove(user);
await repository.remove([
category1,
category2,
category3
]);
delete- Deletes entities by entity id, ids or given conditions:
Example:
await repository.delete(1);
await repository.delete([1, 2, 3]);
await repository.delete({ firstName: "Timber" });
As stated in example here:
import {getConnection} from "typeorm";
await getConnection()
.createQueryBuilder()
.delete()
.from(User)
.where("id = :id", { id: 1 })
.execute();
Which means you should use
removeif it contains an array of Entities.While you should use
deleteif you know the condition.
Additionally, as @James stated in comment Entity Listener such as @BeforeRemove and @AfterRemove listeners only triggered when the entity is removed using repository.remove.
Similarly, @BeforeInsert, @AfterInsert, @BeforeUpdate, @AfterUpdate only triggered when the entity is inserted/updated using repository.save.
Source: Entity Listeners and Subscribers
Answer from Mukyuu on Stack OverflowRepository Remove method deletes the Entity Id
Change removeById method to act like remove method
Question about cascade and remove
Proper way to Delete Object w/ relation
Hello,
I am struggling with something stupid.
I have a post entity which has a one to many relationship with votes
And I have a vote entity.
Basically when I delete the post with .remove() i want all the underlying votes to be deleted as well. But i can't make it work even with cascades. What am I doing wrong?
I get QueryFailedError: update or delete on table "posts" violates foreign key constraint "FK_b5b05adc89dda0614276a13a599" on table "votes"
Hi.
I followed the official documentation at TypeORM and came to the following section.
https://typeorm.io/#/many-to-many-relations
Deleting many to many:
const question = getRepository(Question); question.categories = question.categories.filter(category => { category.id !== categoryToRemove.id }) await connection.manager.save(question)
According to the documentation this should delete the categories and questions connection from the joint table. But whenever I try to follow along I get Property 'categories' does not exist on type 'Question[]'. error showing.
I have followed the documentation to a T, 100% the same, but that error shows up anyway.
What am I missing here?
Thanks.