1

I need to drop a specific table from my database using sequelize.

I tried many times but It deletes all tables.

I want to delete only what I defined as CategoryName.

I try this but not working with me

const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const Category = require('../models/category');

exports.postDeleteCategory = (req,res,next) => {
    const categoryId = req.body.categoryId;
    const categoryName = req.body.categoryName+'s';
    Category.destroy({
        where: {
            id: categoryId
        },
        force: true
        }).then(() => {
            console.log('Destroyed Category');
            sequelize.drop(categoryName+'s');
            res.redirect('/categories');
        })
        .catch(err => {
            console.log(err);
        });
}

It is dropped all tables not what I want.

1 Answer 1

3

This line here drops all the tables.

sequelize.drop(categoryName+'s');

To drop your table just do

await Category.drop()
Sign up to request clarification or add additional context in comments.

2 Comments

I don’t want to drop Category table I want to drop another table by passing name of the table
@othmanali Then do this: sequelize.getQueryInterface().dropTable('tableName')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.