I am using Sequelize with ExpressJS and I have an Organization
model which is as follows:
const Organization = db.define("organization", {
name: {
type: Sequelize.DataTypes.STRING(50),
allowNull: false,
},
});
module.exports = Organization;
This name in this Organization
model is a foreign key in the User model as organization
( The idea is that many users can join an organization - or Many users can join in an organization but an user always belongs to one organization)
The User Model looks like this:
const User = db.define("user", {
first_name: {
type: Sequelize.DataTypes.STRING(50),
allowNull: false,
},
organization: {
type: Sequelize.DataTypes.STRING(25),
allowNull: false,
}
});
User.belongsTo(Organization, {
foreignKey: "organization",
as: "organizationName",
});
However, I am unable to create a new organization first by:
const { name } = req.body;
try {
const organization = await Organization.create({
name,
});
res.status(200).json({
success: true,
created: organization,
});
} catch (error) {
next(new ErrorResponse(`Error! ${error.message}`, 400));
}
It throws me this error: ErrorResponse: Error! Unknown column 'id' in 'field list'
.
However I am able to create a new user with an organization name, and it automatically creates an organization in the organization table.