简体   繁体   中英

Sequelize destroy command

I'm trying delete one record, via destroy method of an instance.

const memCustomers = await Customers.findAll({ limit: 10 })

for (const customer of memCustomers ) {
    //do things
    await customer.destroy()
}

But when i use this command it delete all records in table customers, and when i add "where" argument for destroy method, nothing change...

I set logging equal true... and it show a "delete from table" without "where".

You have two options:

  1. Filter your customers on the app side
const customers = await Customers.findAll({ where: …, limit: … })

Promise.all(customers.filter( … ).map(customer => customer.destroy()))
  1. Call your database again
const customers = await Customers.findAll({ where: …, limit: … })

await Customers.destroy({ where: … })

This highly depends on the number of entries but I would always recommend doing the filtering on the database level

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM