【问题标题】:How I can drop a specific table using sequelize?我如何使用 sequelize 删除特定表?
【发布时间】:2023-02-08 23:52:13
【问题描述】:

我需要使用 sequelize 从我的数据库中删除一个特定的表。

我试了很多次,但它删除了所有表。

我只想删除我定义为 CategoryName 的内容。

我尝试这个但不和我一起工作

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);
        });
}

它删除了所有不是我想要的表。

【问题讨论】:

    标签: node.js express sequelize.js


    【解决方案1】:

    此处的这一行删除了所有表格。

    sequelize.drop(categoryName+'s');
    

    放下你的桌子只是做

    await Category.drop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 2017-09-03
      相关资源
      最近更新 更多