Relationship between Tables
Learn about SQL table relationships and how queries are carried out using TypeORM and NestJS for the API layer.
We'll cover the following
By table relationships, we mean the relationships between various tables or entities as they are described in TypeORM.
Just to review, here are the three basic types of table relationships:
- One-to-one
- One-to-many
- Many-to-many
In this lesson, we’ll look in-depth at the last two and their associated queries.
One-to-one
A one-to-one relationship is one in which an entity uniquely maps to another. A good example is a relationship that exists between a person and their fingerprint.
In TypeORM, we can achieve this using the @OneToOne
decorator. Below, we define two tables with a few properties for both FingerPrint
and Person
classes. We use the @OneToOne
decorator to specify the relationship between them.
We’re also using the @JoinColumn
decorator. This specifies which table we want to include the foreign key. Keep in mind that only one of the tables should include the foreign key.
Get hands-on with 1400+ tech skills courses.