Sequelize One-to-Many Association example with Node.js & MySQL
Sequelize One-to-Many example Overview
In systems analysis, a one-to-many relationship refers to the relationship between two entities A and B in which an element of A may be linked to many elements of B, but a member of B is linked to only one element of A.
For example, assume that you want to design a Tutorial Blog data model, you can think about one Tutorial has many Comments, but one Comment only belongs to one Tutorial.
So the relationship between Tutorial entity and Comment entity is one-to-many. That’s what we’re gonna make in this article, and here are the step by step:
First, we setup Node.js App
Next, configure MySQL database & Sequelize
Define the Sequelize Model and initialize Sequelize
Then we create the Controller for creating and retrieving Entities
Finally we run the app to check the result
Use Sequelize for One-to-Many Association
We’re gonna define the models using Sequelize Model:
const Tutorial = sequelize.define("tutorial", { ... })
const Comment = sequelize.define("comment", { ... })
Tutorial.hasMany(Comment, { as: "comments" });
Comment.belongsTo(Tutorial, {
foreignKey: "tutorialId",
as: "tutorial",
});Using Model.create() for creating new objects:
Then show the tutorial with comments inside using Model.findByPk() and Model.findAll():
The result will look like this:
Sequelize One-to-Many Implementation
Create Node.js App
First, we create a folder:
Next, we initialize the Node.js App with a package.json file:
We need to install necessary modules: sequelize, mysql2.
Run the command:
The package.json file should look like this:
Let’s create Node.js project structure like following directory tree:

Configure Sequelize and MySQL database
In the app folder, we create a separate config folder with db.config.js file like this:
First five parameters are for MySQL connection.
pool is optional, it will be used for Sequelize connection pool configuration:
max: maximum number of connection in poolmin: minimum number of connection in poolidle: maximum time, in milliseconds, that a connection can be idle before being releasedacquire: maximum time, in milliseconds, that pool will try to get connection before throwing error
For more details, please visit API Reference for the Sequelize constructor.
Define the Sequelize Model
In models folder, create tutorial.model.js file like this:
This Sequelize Model represents tutorials table in MySQL database. These columns will be generated automatically: id, title, description, createdAt, updatedAt.
After initializing Sequelize, we don’t need to write CRUD functions, Sequelize supports all of them:
create a new Tutorial:
create(object)find a Tutorial by id:
findByPk(id)get all Tutorials:
findAll()
We’re gonna use Sequelize Model functions in our Controller.
Now continue create Comment model in comment.model.js:
Initialize Sequelize
Let’s initialize Sequelize in app/models folder. Now create app/models/index.js with the following code:
We use hasMany() to help one Tutorial have many Comments, and belongsTo() to indicate that one Comment only belongs to one Tutorial.
Create the Controller
Inside app/controllers folder, let’s create tutorial.controller.js and export these functions:
createTutorial
createComment
findTutorialById
findCommentById
findAll
First we need to import database object:
Create and Save new Tutorials
Create and Save new Comments
Get the comments for a given tutorial
Get the comments for a given comment id
Get all Tutorials include comments
Check the result
Open server.js and write the following code:
– First, we import our database object and controller above.
– Then we call Sequelize sync() method.
In development, you may need to drop existing tables and re-sync database. Just use force: true as following code:
Now, we’re gonna test the result by putting the next lines of code inside run() function.
You can run the this Sequelize one-to-many Node.js application with command: node server.js.
Create Tutorials
Create Comments
Get Tutorial by given id
Get Comment by given id
Get all Tutorials
Check MySQL Database:
– tutorials table:

– comments table:

Last updated
Was this helpful?