乐闻世界logo
搜索文章和话题

How to define entity relationships in TypeORM?

2月17日 22:47

TypeORM defines entity relationships using the following decorators:

  1. @OneToOne(): One-to-one relationship

    • Use @JoinColumn() to specify the foreign key column
    • Example: User and UserProfile
  2. @OneToMany(): One-to-many relationship

    • Defined on the "one" side, pointing to the "many" side
    • Example: Author and Articles
  3. @ManyToOne(): Many-to-one relationship

    • Defined on the "many" side, pointing to the "one" side
    • Example: Article and Author
  4. @ManyToMany(): Many-to-many relationship

    • Requires @JoinTable() to specify the junction table
    • Example: Students and Courses

Cascade operations are configured through the cascade option, with possible values including "insert", "update", "remove", "soft-remove", "recover". For example: @OneToMany(() => Comment, comment => comment.post, { cascade: true })

标签:TypeORM