API with NestJS #73. One-to-one relationships with raw SQL queries

JavaScript NestJS SQL

This entry is part 73 of 77 in the API with NestJS

When designing a database, the tables we define often relate to each other. Managing those relationships is one of the essential parts of working with databases. The previous article taught us how to use raw SQL queries to set up our NestJS project to work with PostgreSQL. In this article, we go a step further and start writing more complex SQL queries involving the one-to-one relationship.

You can find the code from this article in this repository.

The idea behind one-to-one relationship

When designing a one-to-one relationship, a row from the first table has one matching row from the second table and the other way around.

In our case, the address is optional. A one-to-one relationship that is optional might be also referred to as one-to-zero-or-one relationship.

Instead of creating the table, we could add the , , and to the table. However, as the table grows, it might sometimes make sense to split it into more than one table if we can separate a particular group of columns. This might not be the most popular type of relationship, but we might encounter it when working with various databases. Because of that, we go through how to set it up and work with it.

When deciding on whether to create a one-to-one relationship we can take a lot of factors into consideration. If you want to read more, check out this question on StackOverflow.

Working with a one-to-one relationship

A very straightforward example involves a user and an address. Let’s start by creating a new migration to define the addresses table and connect it to the users.


Above, we add the column to the table. In our application, a particular address can belong only to one user. Because of that, we add the unique constraint to the column. When we do that, we ensure that only one user can refer to a particular address. Trying to tie more than one user to the same address would result in an error.

We also make the column a foreign key that refers to the primary key of the table. Thanks to that, we make sure that PostgreSQL knows there is a connection.

Inserting two entities in a single query

We want to be able to insert the user and the address into the database at the same time. A good way to do that is by using the statement.


In this article, we use authentication with JWT. If you want to know more, check out API with NestJS #3. Authenticating users with bcrypt, Passport, JWT, and cookies

Above, we create a Common Table Expression query using the statement. Thanks to that, we create both the address and the user in a single SQL that is atomic. If something goes wrong when creating the user, the address won’t be persisted in the database.

Expanding the models

As our queries get more complex, the models grow bigger too. So let’s first create a model for the address.


Above, I create the model and define the constructor manually. You can use the class-transformer library for that, if you prefer.

The last step in creating the models is to use the above class in the model of the user.


Querying two tables

Our queries can fetch rows from multiple tables at once and match them. A good way of doing that is with a query.

When we use the keyword, we perform the inner join. The crucial thing is that it returns records with matching values in both tables. In our case, the address is optional. Running the above query for a user that does not have the address would return nothing.

To fix the issue, we need to perform the outer join. Outer joins preserve the rows that don’t have matching values. In our case, we want to use the that returns all records from the left table and the matched records from the right table. In our case, the left table is , and the right table is the .


Thanks to the above approach, our query works correctly for users that don’t have the address.

Returning the related entity when inserting

Before, we used the statement to create the user and the address in a single query. We can use two statements to return the data from both tables.


Thanks to the above, our API returns the user together with the address when signing up.


In this article, we’ve gone through the idea behind one-to-one relationships. We’ve also learned to work with them using PostgreSQL and raw SQL queries. When doing that, we had to write common table expressions to make sure we created two entities in a single query. We’ve also identified the difference between inner and outer joins. There is still much to learn regarding relationships in PostgreSQL, so stay tuned!

Series Navigation<< API with NestJS #72. Working with PostgreSQL using raw SQL queriesAPI with NestJS #74. Designing many-to-one relationships using raw SQL queries >>
Notify of
Inline Feedbacks
View all comments