Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
Sequelize bull create with a common id based on previous inserted data

I am trying to insert the following data using sequelize and nodejs into my db

{
  "basicDetails": {
    "companyName": "demo",
    "email": "demo5@demo.com",
    "phone": "5019918819",
    "address": "manab",
    "pincode": "400078",
    "city": "Mumbai",
    "state": "Maharashtra"
  },
  "subOffices": [
    {
      "subofficename": "bpo",
      "location": "random"
    },
    {
      "subofficename": "fintech",
      "location": "worli"
    }
  ],
  "companyUsers": [
    {
      "userName": "Random user",
      "userEmail": "random@us.com",
      "userPhone": "0000000000"
    },
    {
      "userName": "Demo User",
      "userEmail": "demo@us.com",
      "userPhone": "1111111111"
    }
  ]
}

I have basically 3 tables :-

  1. Client (id)
  2. SubOffices (foreignKey is the client id)
  3. CompanyUsers (foreignKey is the client id)

basic details would be stored in a client table and using the id of that data. I would like to insert the sub offices and company users in there respective table where foreign key would be the id that I get from inserting my basic details into the client table

I am able to insert the basic details to the client table and fetch the id of that data . I would further like to do a bulk insert using sequelize including the id that I am getting from the client data as a foreign key to it.

router.post("/addClient", async function (req, res) {
  const basicdetails = req.body.basicDetails;
  console.log(basicdetails);
  const subofficesData = [...req.body.subOffices];
  const companyusers = [...req.body.companyUsers];
  console.log("suboffices: " + suboffices);
  subofficesData.forEach((item) => console.log(item));
  companyusers.forEach((item) => console.log(item));

  const data = await client.create({
    companyName: basicdetails.companyName,
    email: basicdetails.email,
    mobileNumber: basicdetails.phone,
    address: basicdetails.address,
    pincode: basicdetails.pincode,
    city: basicdetails.city,
    state: basicdetails.state,
  });

  console.log(data.id);

  const subofficesResult = await suboffices.bulkCreate(subofficesData, {
    clientId: data.id,
  });
  console.log(subofficesResult);

  //   console.log(req.body.suboffices);
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This doesn't actually look like a scenario where you want to use bulkCreate at all.

Look into utilizing the include property in the options object of your create method.

https://sequelize.org/master/manual/creating-with-associations.html

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda