Relations
What are relations
Relations helps you to work with related entities easily. There are several types of relations:
one-to-one using
@OneToOne
many-to-one using
@ManyToOne
one-to-many using
@OneToMany
many-to-many using
@ManyToMany
Relation options
There are several options you can specify for relations:
eager: boolean
- If set to true, the relation will always be loaded with the main entity when usingfind*
methods orQueryBuilder
on this entitycascade: boolean | ("insert" | "update")[]
- If set to true, the related object will be inserted and updated in the database. You can also specify an array of cascade options.onDelete: "RESTRICT"|"CASCADE"|"SET NULL"
- specifies how foreign key should behave when referenced object is deletednullable: boolean
- Indicates whether this relation's column is nullable or not. By default it is nullable.orphanedRowAction: "nullify" | "delete" | "soft-delete" | disable
- When a parent is saved (cascading enabled) without a child/children that still exists in database, this will control what shall happen to them. delete will remove these children from database. soft-delete will mark children as soft-deleted. nullify will remove the relation key. disable will keep the relation intact. To delete, one has to use their own repository.
Cascades
Cascades example:
As you can see in this example we did not call save
for category1
and category2
. They will be automatically inserted, because we set cascade
to true.
Keep in mind - great power comes with great responsibility. Cascades may seem like a good and easy way to work with relations, but they may also bring bugs and security issues when some undesired object is being saved into the database. Also, they provide a less explicit way of saving new objects into the database.
Cascade Options
The cascade
option can be set as a boolean
or an array of cascade options ("insert" | "update" | "remove" | "soft-remove" | "recover")[]
.
It will default to false
, meaning no cascades. Setting cascade: true
will enable full cascades. You can also specify options by providing an array.
For example:
@JoinColumn
options
@JoinColumn
options@JoinColumn
not only defines which side of the relation contains the join column with a foreign key, but also allows you to customize join column name and referenced column name.
When we set @JoinColumn
, it automatically creates a column in the database named propertyName + referencedColumnName
. For example:
This code will create a categoryId
column in the database. If you want to change this name in the database you can specify a custom join column name:
Join columns are always a reference to some other columns (using a foreign key). By default your relation always refers to the primary column of the related entity. If you want to create relation with other columns of the related entity - you can specify them in @JoinColumn
as well:
The relation now refers to name
of the Category
entity, instead of id
. Column name for that relation will become categoryName
.
You can also join multiple columns. Note that they do not reference the primary column of the related entity by default: you must provide the referenced column name.
@JoinTable
options
@JoinTable
options@JoinTable
is used for many-to-many
relations and describes join columns of the "junction" table. A junction table is a special separate table created automatically by TypeORM with columns that refer to the related entities. You can change column names inside junction tables and their referenced columns with @JoinColumn
: You can also change the name of the generated "junction" table.
If the destination table has composite primary keys, then an array of properties must be sent to @JoinTable
.
Last updated