0

I have a Job Model

    module.exports = (sequelize, Datatypes) => {
  const Jobs = sequelize.define('Jobs', {
    jobinput: {
      type: Datatypes.STRING
    },
    typeinput: {
      type: Datatypes.STRING
    },
    jobdescinput: {
      type: Datatypes.TEXT('medium')
    },
    locationinput: {
      type: Datatypes.STRING
    },
    salarymininput: {
      type: Datatypes.INTEGER
    },
    salarymaxinput: {
      type: Datatypes.INTEGER
    },
    seniorityinput: {
      type: Datatypes.STRING
    },
    vacancyinput: {
      type: Datatypes.INTEGER
    },
    bountyinput: {
      type: Datatypes.INTEGER
    }
  })
  Jobs.associate = function (models) {
    Jobs.belongsTo(models.User)
    Jobs.belongsTo(models.Company)
    Jobs.belongsToMany(models.JobSkills, { through: models.JobHasSkills, foreignKey: 'JobId' })
  }

  return Jobs
}

and a JobSkill Model

module.exports = (sequelize, Datatypes) => {
  const JobSkills = sequelize.define('JobSkills', {
    skill: {
      type: Datatypes.STRING,
      unique: true
    }
  })
  JobSkills.associate = function (models) {
    JobSkills.belongsToMany(models.Jobs, { through: models.JobHasSkills, foreignKey: 'JobSkillId' })
  }
  return JobSkills
}

and a Many to Many JobHasSkills Model

module.exports = (sequelize, Datatypes) => {
  const JobHasSkills = sequelize.define('JobHasSkills', { })
  JobHasSkills.associate = function (models) {
    JobHasSkills.belongsTo(models.Jobs)
    JobHasSkills.belongsTo(models.JobSkills)
  }
  return JobHasSkills
}

and I was trying to do a create function

async create (req, res) {
   try {
     await Jobs.create(req.body, { include: { model: JobSkills } })
       .then(function (createdObjects) {
         res.json(createdObjects)
       })
   } catch (err) {
     res.status(500).send({
       error: 'an error has occured while posting'
     })
   }
 }

My Json data that i sent through postman is

    { "jobinput": "Title",
     "typeinput": "Part Time",
     "jobdescinput": "lorem ipsum",
     "locationinput": "California",
     "salarymininput": "0",
     "salarymaxinput": "1000",
     "seniorityinput": "Internship",
     "vacancyinput": "1",
     "bountyinput": "10",
     "skills": ["skill1","skill2","skill3" ] 
   }

I checked this answer Trying to create an instance and multiple related instances in a many to many relationship

But I was getting the catch(error). This is my first project with node js and sequelize. Can anyone help me on this

  • error i get is SequelizeEagerLoadingError. and jobs table is created while jobskills and jobhasskills are not – Tony Davis Feb 26 '19 at 19:18

1 Answers1

0

If your tables are not getting created than please use -

 sequelize.define(
    "Table Name"
    {
      Model Definations
    },
    {
        underscored: false,// Not Required in your case
        timestamps: false// Not Required in your case
    }
).sync({ force: true });

Once tables are created do comment the Sync option otherwise on every restart tables will be dropped and recreated.

Rohit Dalal
  • 856
  • 9
  • 12