0

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.

1 Answer 1

1

I solved it by adding an id column to the table.

1
  • 2
    Please stick with answer, don't post counter question.
    – 4b0
    Commented Aug 25, 2021 at 6:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.