Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple rows in table from array?

Is possible to add multiple rows at once from array with sequelize.js? This is my code:

  var user = User.build({
    email: req.body.email,
    password: req.body.password,
    userlevel: '3',
  });

  User
  .find({ where: { email: req.body.email } })
  .then(function(existingUser){

    if (existingUser) {
      return res.redirect('/staff');
    }

    user
    .save()
    .complete(function(err){
      if (err) return next(err);
      res.redirect('/staff');
    });
  }).catch(function(err){
    return next(err);
  });

Thanks for any advise!

like image 688
Makromat Avatar asked Nov 19 '14 10:11

Makromat


People also ask

How do I add multiple rows to a table?

Tip: To insert more than one row (or column) at the same time, select as many rows or columns as you want to add before you click the insert control. For example, to insert two rows above a row, first select two rows in your table and then click Insert Above.

Can you add multiple rows at once in SQL?

Answer. Yes, instead of inserting each row in a separate INSERT statement, you can actually insert multiple rows in a single statement. To do this, you can list the values for each row separated by commas, following the VALUES clause of the statement.

Can we insert multiple rows single insert statement?

INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.


1 Answers

https://sequelize.readthedocs.io/en/v3/docs/instances/#working-in-bulk-creating-updating-and-destroying-multiple-rows-at-once

User.bulkCreate([{ /*  record one */ }, { /* record two */ }.. ]) 
like image 108
Jan Aagaard Meier Avatar answered Sep 22 '22 02:09

Jan Aagaard Meier