我有一个项目,其中有两个主表:Contact和Worker

我还有一个名为WorkerContact的连接表

在我的项目中,我为用户提供了删除联系人的选项,这也需要删除联接表的元素.我担心的是,在我当前的设置中(见下文),如果我遇到一个错误,我成功删除了一个联系人,但随后又未能删除相关的连接表(由于错误),那么一切都会被打乱.所以我的问题是,有没有一种方法可以重构这一点,以确保在执行实际删除之前完成这两项操作,然后将promise 发送到前端?

以下是我目前的情况:

前端:

export const destroyContact = (contact_id) => dispatch => {
  
  axios.post(`http://localhost:3001/contacts/destroy`, {id: contact_id})
  .then(() => {
    dispatch({type: 'CONTACT_DESTROYED', payload: contact_id});
    axios.post(`http://localhost:3001/workerContacts/destroy`, {id: contact_id}) //I'm scared that the first thing will run but the second one won't, causing a lot of problems. We can deal with this by just throwing a big error message for the user hopefully
    .then(() => {
      dispatch({type: 'JOIN_TABLE_ROWS_DESTROYED', payload: contact_id});
    })
    .catch(err => dispatch({type: 'ERROR_CAUGHT', payload: {err_message: err.response.data.message, err_code: err.response.request.status, err_value: err.response.request.statusText}}))
  
    })
  .catch(err => dispatch({type: 'ERROR_CAUGHT', payload: {err_message: err.response.data.message, err_code: err.response.request.status, err_value: err.response.request.statusText}}))
}

我也在使用redux,所以这就是为什么我有所有的dispatch等等,但本质上我已经将删除拆分为两个axios调用:一个是我删除联系人,另一个是删除连接表.

后端:

对于联系人,我有以下内容:

export const destroy = (req, res) => {
    // Here is when we want to remove an existing contact
    Contact.deleteMe(req.body.id)
    .then(() => res.json("Contact deleted"))
    .catch((err) => res.status(500).json({message: "Something went wrong when trying to save delete this. Try and reload the page and try again "}))
}

和关联的DeleteMe函数:

static deleteMe(customer_id){
        //Uses SQL to delete an individual customer element
        return db.execute('DELETE FROM contacts WHERE id = ?', [customer_id]);
    }

对于可接合的桌子,我有这样的 idea :

export const destroy = (req, res) => {
    // Here is when we want to remove an existing contact
    JoinTable.deleteMe(req.body.id)
    .then(() => res.json("Join tables deleted"))
    .catch(err => res.status(500).json({message: "Something went wrong on our end. Try to reload the page and start again"}))
}

和关联的DeleteMe函数:

static deleteMe(customer_id){
        //Uses SQL to delete an individual customer element
        return db.execute('DELETE FROM workercontacts WHERE workerContacts.contact_id = ?', [customer_id]);
    }

如果有帮助的话,我正在使用MySQL数据库.

希望这是足够的信息,但如果您需要更多,我肯定可以提供给您.

推荐答案

只需使用一个调用并在一个事务中执行DELETE个命令:

export const destroyContact = (contact_id) => (dispatch) => {
  axios
    .post(`http://localhost:3001/contacts/destroy`, { id: contact_id })
    .then(() => {
      dispatch({ type: 'CONTACT_DESTROYED', payload: contact_id });
      dispatch({ type: 'JOIN_TABLE_ROWS_DESTROYED', payload: contact_id });
    })
    .catch((err) =>
      dispatch({
        type: 'ERROR_CAUGHT',
        payload: {
          err_message: err.response.data.message,
          err_code: err.response.request.status,
          err_value: err.response.request.statusText,
        },
      })
    );
};

其中一个后端:

static async function deleteMe(customer_id) {
    await db.execute('START TRANSACTION');
    try {
        await db.execute('DELETE FROM contacts WHERE id = ?', [customer_id]);
        await db.execute('DELETE FROM workercontacts WHERE workerContacts.contact_id = ?', [customer_id]);
        await db.execute('COMMIT');
    } catch (err) {
        await db.execute('ROLLBACK');
    }
}

...

export const destroy = (req, res) => {
    // Here is when we want to remove an existing contact
    Contact.deleteMe(req.body.id)
        .then(() => res.json("Contact deleted"))
        .catch((err) => res.status(500).json({message: "Something went wrong when trying to save delete this. Try and reload the page and try again "}))
}

Mysql相关问答推荐

括号在SQL查询中的作用?

—符号在哪里条件""

在联合查询中使用GROUP BY和ORDER BY

优化使用ORDER BY加载耗时较长的MySQL请求

如果我有USER列,如何将SQL CREATE TABLE与PostgreSQL和MySQL一起使用?

生成json数据并根据特定表的匹配条件进行过滤

关于设置为 NOT NULL 时的 CHAR 默认值

如何使用mysql更新列中的json数据

如何在 mysql 中获取 Day Wise 的平均值?

检索按键列值分组的最新日期 (MySql)

如何根据 R 中的价格范围将数据从一列复制到新列?

为什么以及如何将 Django filter-by BooleanField 查询转换为 SQL WHERE 或 WHERE NOT 而不是 1/0?

处理 Visits 表中数百万行的最佳方法是什么?

从 SQL 中的左连接和内连接中减go 计数

为什么在有 BEGIN 和 END 时为存储过程指定分隔符?

如何按给定开始日期和结束日期的月份汇总?

MySQL中两个时间字段的分钟差

是否可以在 macOS 上只安装 mysqldump

MySQL - 我如何输入 NULL?

phpMyAdmin - 无法连接 - 无效设置 - 自从我添加了 root 密码 - 被锁定