Please help solve the problem. there are department and employee models
module.exports = (sequelize, DataTypes) => {
var Department = sequelize.define('Department', {
name: DataTypes.STRING,
description: DataTypes.STRING
});
Department.associate = function(models) {
Department.hasMany(models.employee);
};
return Department;
};
module.exports = (sequelize, DataTypes) => {
var Employee = sequelize.define('Employee', {
name: DataTypes.STRING,
age: DataTypes.INTEGER,
depart: DataTypes.STRING
});
Employee.associate = function (models) {
Employee.belongsTo(models.department, {
foreignKey: {
allowNull: false
}
});
};
return Employee;
};
Associations are also written there that one department can have several employs, one employ has only one department.
further in the migration I add DepartmentId
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable("Employees", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
age: {
type: Sequelize.INTEGER
},
depart: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
},
DepartmentId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Departments',
key: 'id'
}
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("Employees");
}
};
further in the controller, I call the function, which should create in my database the emails with DepartmentId
module.exports.createEmployee = async (req, res) => {
if (!req.body) {
return res.status(400).json({ massage: "Employee body is required"
});
}
try {
const employee = await Employee.create({
name: req.body.name,
age: req.body.age,
depart: req.body.depart,
DepartmentId: req.body.DepartmentId
});
I'm sending a request through Postman
{ "name": "teqwest1", "age": "121", "depart": "qwew1qweqweqe", "DepartmentID" : "1" }
leading to an error
original: { error: null value in column "DepartmentId" violates not-null constraint at Connection.parseE (/home/vitalik/node/node_modules/pg/lib/connection.js:604:11) at Connection.parseMessage (/home/vitalik/node/node_modules/pg/lib/connection.js:401:19) at Socket. (/home/vitalik/node/node_modules/pg/lib/connection.js:121:22) at Socket.emit (events.js:198:13) at addChunk (_stream_readable.js:288:12) at readableAddChunk (_stream_readable.js:269:11) at Socket.Readable.push (_stream_readable.js:224:10) at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) name: 'error', length: 280, severity: 'ERROR', code: '23502', detail: 'Failing row contains (62, teqwest1, 121, qwew1qweqweqe, 2019-10-11 14:51:37.856+00, 2019-10-11 14:51:37.856+00, null).', hint: undefined, position: undefined, internalPosition: undefined, internalQuery: undefined, where: undefined, schema: 'public', table: 'Employees', column: 'DepartmentId', dataType: undefined, constraint: undefined, file: 'execMain.c', line: '2017', routine: 'ExecConstraints', sql: 'INSERT INTO "Employees" ("id","name","age","depart","createdAt","updatedAt") VALUES (DEFAULT,$1,$2,$3,$4,$5) RETURNING *;', parameters: [ 'teqwest1', '121', 'qwew1qweqweqe', '2019-10-11 14:51:37.856 +00:00', '2019-10-11 14:51:37.856 +00:00' ] }, sql: 'INSERT INTO "Employees" ("id","name","age","depart","createdAt","updatedAt") VALUES (DEFAULT,$1,$2,$3,$4,$5) RETURNING *;', parameters: [ 'teqwest1', '121', 'qwew1qweqweqe', '2019-10-11 14:51:37.856 +00:00', '2019-10-11 14:51:37.856 +00:00' ] }
Stack:
Nodejs
Express
Postgres
Sequelize
How to solve this problem, what would be created in a database that has a foreign key DepartmentId?