Sequelize One-To-One association – NodeJS/Express, MySQL


In the post, we got started with Sequelize ORM. Today we’re gonna create Sequelize One-To-One association models with NodeJS/Express, MySQL.

Related posts:
Sequelize ORM – Build CRUD RestAPIs with NodeJs/Express, Sequelize, MySQL
Sequelize One-To-Many association – NodeJS/Express, MySQL
Sequelize Many-to-Many association – NodeJS/Express, MySQL

Sequelize One-to-One association

Note: When calling a method such as Customer.hasOne(Address), we say that the Customer model is the source and the Address model is the target.

We define 2 models:

How to create One-to-One association between them?
-> Sequelize provides 2 ways:



What is difference between belongsTo and hasOne?
-> hasOne inserts the association key in target model whereas BelongsTo inserts the association key in the source model.

We can create a solution by combine foreignKey and targetKey with belongsTo and hasOne relation as below code:

Foreign Key

– In belongsTo relation, foreign key will be generated from the target model name and the target primary key name. Sequelize provides a foreignKey option to override defaultValue.

Target Key

– The target key is the column on the target model that the foreign key column on the source model points to. In belongsTo relation, by default the target key will be the target model’s primary key. Sequelize provides a targetKey option to define a custom column.

How to save it?

How to fetch entities?

-> Way to get all Customers that includes Addresses:

With attributes option, we can select only some attributes:

And attributes can be renamed using a nested array:


We create a NodeJs/Express project as below structure:

Setting up NodeJs/Express project

Following the guide to create a NodeJS/Express project.

Install Express, Sequelize, MySQL:

-> package.json file:

Setting up Sequelize MySQL connection

– Create ‘./app/config/env.js’ file:

– Setup Sequelize-MySQL connection in ‘./app/config/db.config.js’ file:

Define Sequelize models

– Address model:

– Customer model:

Express RestAPIs

Define Customer’s routes in ‘./app/controller/customer.route.js’ file:


Implement Customer’s controller in ‘./app/controller/customer.controller.js’ file:


Run & Check results

Start NodeJs server:

Use Postman to check result ->

– POST customers:

Sequelize One-to-One association - CRUD RestAPI NodeJSExpress MySQL - post request

Check database after do POST:

Sequelize One-to-One association - CRUD RestAPI NodeJSExpress MySQL - customer table

Sequelize One-to-One association - CRUD RestAPI NodeJSExpress MySQL - address table

– Find All Customers:

Sequelize One-to-One association - CRUD RestAPI NodeJSExpress MySQL - get all customers request



By grokonez | April 20, 2018.

Last updated on September 28, 2018.

Related Posts

4 thoughts on “Sequelize One-To-One association – NodeJS/Express, MySQL”

  1. this line of code is giving error saying “col is not defined”;
    where: { fk_customerid: db.Sequelize.col(‘customer.uuid’) },

Got Something To Say:

Your email address will not be published. Required fields are marked *