添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Do cascade options in TypeORM overlap or do they have a completely different purpose? Their description in the documentation is very scarce and partly missing, or I couldn't find it.

IOW, do the following options

{ cascade: "update" } = { onUpdate: 'CASCADE' }

{ cascade: "remove" } = { onDelete: 'CASCADE' }

have the same effect?

Or the cascade option is only for the TypeORM use while onUpdate and onDelete are only for the DB schema (created by migration)?

This is my conclusion of looking into it:

The cascade option does not affect the database column constraints, and I believe is used by TypeORM only in evaluating how to save entity relations to the database. We can define entities like this:

@Entity()
class Book extends BaseEntity {
    @ManyToOne(() => Author, (author) => author.books, {
        onDelete: 'CASCADE',
    public author?: Author
@Entity()
class Author extends BaseEntity {
    @OneToMany(() => Book, (book) => book.author, {
        cascade: true,
    public books: Book[];

onDelete sets the authorId foreign key to CASCADE onDelete on Book. This means that when the author is deleted, the book is also deleted.

Setting cascade: true on Author tells TypeORM that if a new book is appended on an author and the author is saved, the new book should also be saved to the database. Like this:

const author = await Author.findOne({ id: '123' });
author.books.push(new Book(...));
await author.save();

If cascade is not set on Book, the new book will not be saved to the database.

FYI: as of now cascade got extended. It is not just insert and update only anymore. Current options: ["insert", "update", "remove", "soft-remove", "recover"] – GeriTol Dec 2, 2020 at 15:14 The cascade option DOES affect the foreign key constraint. For this example, it will add "ON DELETE CASCADE" to the foreign key constraint of author → books for mysql. – Shunya Watanabe Sep 7, 2022 at 0:06 I don't see how one can come to the conclusion that cascade delete isn't supported based on the issue @jstnno links to? The issue mention a bug that was fixed in 2019! – Stian Jørgensrud Dec 10, 2021 at 14:37

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.