What is relationship Relationships can help you easily collaborate with related entities. There are several types of relationships One-to-one use @OneToOne Many-to-one use @ManyToOne One-to-many use @OneToMany Many to many use @ManyToMany Relationship options eager: boolean- If set to true , when using find * or QueryBuilder on this entity, the relationship will always be loaded with the main entity cascade: boolean- If set to true , related objects will be inserted and updated in the database. onDelete: "RESTRICT"|"CASCADE"|"SET NULL" -Specify the behavior of foreign keys when deleting referenced objects primary: boolean- indicates whether the column of this relationship is the primary column. nullable: boolean- indicates whether the column of this relationship is nullable. It is nullable by default. orphanedRowAction: "nullify" | "delete"-After deleting a child row from its parent row, determine whether the child row is orphaned (default value) or deleted.
. One is an A contains only a B instance, and B contains only one A relationship instance. Let's take the User and Profile entities as examples.
A user can only have one profile, and a profile can only be owned by one user.
Here we add @OneToOne to the profile and specify the target relationship type as Profile . We also added @JoinColumn , which is mandatory and can only be set on one side of the relationship. Which side of the @JoinColumn you set , the table of which side will contain a "relation id" and a foreign key to the target entity table. Similarly, @JoinColumn must be set only on one side of the relationship and must be on the side with the foreign key in the database table.
Many-to-one / one-to-many relationship Many to one / many means A comprises a plurality of B example of relationship, but B contains only one A instance. Let us take the User and Photo entities as examples. User can have multiple photos , but each photo only by a user owns
Here we add @OneToMany to the photos attribute and specify the target relationship type as Photo . You can @ManyToOne / @OneToMany omitted relations @JoinColumn , Unless you need to customize the name of the association column in the database. @ManyToOne can be used alone, but @OneToMany must be used with @ManyToOne . If you want to use @OneToMany , you need @ManyToOne . Where you set @ManyToOne , related entities will have " association id" and foreign keys. Need to check with the photos of the User , must FindOptions specified relations
Many-to-many relationship Many-to-many is a relationship in which A contains multiple instances of B , and B contains multiple instances of A. We take Question and Category entities as examples. Question can have multiple categories, each category can have multiple questions
@JoinTable() is required for @ManyToMany relationship
To load questions in categories , you must specify the relationship in FindOptions
Use Query Builder
QueryBuilder is one of the most powerful features of TypeORM , it allows you to use elegant and convenient syntax to build SQL queries, execute and obtain automatically converted entities.