Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: Where is an example of using bulkDelete with criteria?

I'm trying to write a seed file's down and I'd like to bulkDelete the data I created in my up. But I can't find any documentation on how to do this. The official docs don't give an example: http://docs.sequelizejs.com/class/lib/query-interface.js~QueryInterface.html#instance-method-bulkDelete

Can someone show me how to bulkDelete all rows in table Foo where name equals x or name equals y?

  down: (queryInterface, Sequelize) => {
    return queryInterface.bulkDelete('Foo', [what do I put here?], {});
  }
like image 474
Daniel Kaplan Avatar asked Jan 12 '18 19:01

Daniel Kaplan


2 Answers

I tried using @Michael McCabe's suggestion and kept getting ERROR: Invalid value [object Object]. Once I got rid of where from the second argument (the identifier argument), I was able to get it working:

down: (queryInterface, Sequelize) => {
  const Op = Sequelize.Op
  return queryInterface.bulkDelete('users', {id: {[Op.in]: [2, 3]}}, {})
}
like image 148
kaszac Avatar answered Oct 10 '22 05:10

kaszac


down: (queryInterface, Sequelize) => {
  const Op = Sequelize.Op; 

  return queryInterface.bulkDelete(
    'Foo',
    {[Op.or]: [{name: x}, {name: y}]}
  );
}

1st arg is the table name, 2nd arg is the where value that indicates which rows to delete.

like image 20
Michael McCabe Avatar answered Oct 10 '22 04:10

Michael McCabe