Sequelize one-to-many relation can be defined using belongsTo and hasMany methods. but before moving on to one-to-many I suggest readers to go through my previous post on one-to-one relation.
Defining One-To-One relation in Sequelize
Now let us consider a school management system when we define relation between Subjects and Chapters. One subject has many chapters. The database schema looks as follows.
Master Subjects Entity
const MasterAcademicsModel = require('../Models/MasterAcademicsModel');
const MasterBoardsModel = require('../Models/MasterBoardsModel');
const MasterClassModel = require('../Models/MasterClassModel');
const MasterMediumsModel = require('../Models/MasterMediumsModel');
const MasterSubjectModel = db.define(
"master_subjects",
{
subjectId: {
field: "subjectId",
type: sqlOperator.UUID,
primaryKey: true,
defaultValue: sqlOperator.UUIDV4,
},
name: {
field: "name",
type: sqlOperator.STRING(100),
allowNull: false,
},
},
{
tableName: "master_subjects",
timestamps: true,
}
);
MasterSubjectModel.associate = async () =>{
await MasterSubjectModel.belongsTo(MasterAcademicsModel,{
foreignKey: 'masterSubjectAcademicId',
targetKey: 'academicId'
});
await MasterSubjectModel.belongsTo(MasterBoardsModel,{
foreignKey: 'masterSubjectBoardId',
targetKey: 'boardId'
});
await MasterSubjectModel.belongsTo(MasterMediumsModel,{
foreignKey: 'masterSubjectMediumId',
targetKey: 'mediumId'
});
await MasterSubjectModel.belongsTo(MasterClassModel,{
foreignKey: 'masterSubjectClassId',
targetKey: 'classId'
});
}
module.exports = MasterSubjectModel;
Chapters Model
const ChapterModel = db.define(
"chapters",
{
chapterId: {
field: "chapterId",
type: sqlOperator.UUID,
primaryKey: true,
defaultValue: sqlOperator.UUIDV4,
},
title: {
field: "title",
type: sqlOperator.STRING(100),
},
tags: {
field: "tags",
type: sqlOperator.STRING(100),
},
},
{
tableName: "chapters",
timestamps: true,
}
);
ChapterModel.associate = async () => {
console.log(
"--- Establishing relations between chapter and chapter content ---"
);
const MasterSubjectModel = require("./MasterSubjectModel");
const ChapterContentModel = require("./ChapterContentModel");
const MasterAcademicsModel = require("./MasterAcademicsModel");
await ChapterModel.belongsTo(MasterSubjectModel, {
foreignKey: "masterSubjectId",
targetKey: "subjectId",
as: "subject",
});
await MasterSubjectModel.hasMany(ChapterModel, {
foreignKey: 'masterSubjectId',
sourceKey: 'subjectId',
});
await ChapterModel.belongsTo(MasterAcademicsModel, {
foreignKey: "masterAcademicId",
targetKey: "academicId",
as: "academic",
});
await MasterAcademicsModel.hasMany(ChapterModel, {
foreignKey: 'masterAcademicId',
sourceKey: 'academicId',
});
await ChapterModel.hasMany(ChapterContentModel, {
foreignKey: 'chapterId',
sourceKey: 'chapterId',
as: "chapterContents",
});
await ChapterContentModel.belongsTo(ChapterModel, {
foreignKey: "chapterId",
targetKey: "chapterId",
});
};
module.exports = ChapterModel;
Notice here that to define One-To-Many both belogsTo and hasMany method are used. you can define the relation using only belongsTo method, however you will endup with multiple sequelize errors. To avoid sequelize errors while fetching data using find method on one-to-many relation, kindly update your models by defining relation in both ways.