Reputation: 1068
I am just learning how to use Sequelize in NodeJs to access MySQL database but I got stuck with the foreign key thing while creating the models. Can anyone suggest how shall I apply as I had tried one but it threw error.
Models
exam_category.js
const Sequelize = require('sequelize');
const sequelize = require('../../database');
const ExamCategory = sequelize.define('exam_category', {
id:{ type: Sequelize.BIGINT, autoIncrement: true, allowNull: false, primaryKey: true },
exam_category_name: { type: Sequelize.STRING, allowNull: false },
isActive: { type: Sequelize.BOOLEAN, allowNull: false },
createdBy: { type: Sequelize.STRING, allowNull: true },
createdAt: { type: Sequelize.DATE, allowNull: false }
}, { timestamps: false });
module.exports = ExamCategory;
exam.js
const Sequelize = require('sequelize');
const sequelize = require('../../database');
const Exam = sequelize.define('exam', {
id:{ type: Sequelize.BIGINT, autoIncrement: true, allowNull: false, primaryKey: true },
startDate: { type: Sequelize.STRING, allowNull: false },
endDate: { type: Sequelize.STRING, allowNull: false },
startTime: { type: Sequelize.STRING, allowNull: false },
endTime: { type: Sequelize.STRING, allowNull: false },
examDuration: { type: Sequelize.INTEGER, allowNull: false },
examName: { type: Sequelize.STRING, allowNull: false },
examKey: { type: Sequelize.STRING, allowNull: false, unique: true },
exam_category_id: { type: Sequelize.BIGINT, allowNull: false, references: 'exam_category', referencesKey: 'id' },
isActive: { type: Sequelize.BOOLEAN, allowNull: false },
createdBy: { type: Sequelize.STRING, allowNull: true },
createdAt: { type: Sequelize.DATE, allowNull: false }
}, { timestamps: false });
module.exports = Exam;
Then in app.js I wrote this one:
const sequelize = require('./database');
const adminUser = require('./models/users/admin_user');
const examCategory = require('./models/users/exam_category');
const exam = require('./models/users/exam');
examCategory.ExamCategory.hasMany(exam.Exam, {
foreignKey: 'exam_id',
sourceKey: 'id'
});
exam.Exam.belongsTo( examCategory.ExamCategory, {
foreignKey: 'exam_id',
targetKey: 'id'
});
sequelize.sync();
sequelize.sync({ force: true });
And this is my database.js
const Sequelize = require('sequelize');
const examCategory = require('./exam_category');
const db = new Sequelize(
'otp_node', 'root', 'root',
{
dialect: 'mysql',
host: 'localhost'
}
);
module.exports = db;
ERROR
PS D:\practice\otp\frontend> node .\server.js internal/modules/cjs/loader.js:968 throw err; ^
Error: Cannot find module './exam_category' Require stack:
- D:\practice\otp\frontend\backend\database.js
- D:\practice\otp\frontend\backend\app.js
- D:\practice\otp\frontend\server.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15) at Function.Module._load (internal/modules/cjs/loader.js:841:27) at Module.require (internal/modules/cjs/loader.js:1025:19) at require (internal/modules/cjs/helpers.js:72:18) at Object. (D:\practice\otp\frontend\backend\database.js:2:22)
at Module._compile (internal/modules/cjs/loader.js:1137:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10) at Module.load (internal/modules/cjs/loader.js:985:32) at Function.Module._load (internal/modules/cjs/loader.js:878:14) at Module.require (internal/modules/cjs/loader.js:1025:19) { code: 'MODULE_NOT_FOUND', requireStack: [ 'D:\practice\otp\frontend\backend\database.js', 'D:\practice\otp\frontend\backend\app.js', 'D:\practice\otp\frontend\server.js' ] } PS D:\practice\otp\frontend>
For reference this is my Github Repo of the code. Can anyone help me with this ?
Upvotes: 0
Views: 1358
Reputation: 3626
you can try this to add foreign Key in your table
app.js
const sequelize = require('./database');
const adminUser = require('./models/users/admin_user');
const ExamCategory = require('./models/users/exam_category');
const Exam = require('./models/users/exam');
ExamCategory.hasMany(exam, {
foreignKey: 'exam_id',
sourceKey: 'id'
});
Exam.belongsTo( examCategory, {
foreignKey: 'exam_id',
targetKey: 'id'
});
sequelize.sync();
sequelize.sync({ force: true });
add above code in your file where your are associating the tables it will add foreign_key in your table as name exam_id you can give it name whatever you want
you can know more about foreignKey and relationship between tables in this doc
check this repository so you can understand how to use sequelize and also a boilerplate of node sequelize
Upvotes: 1