【问题标题】:How to Insert Into table with multiple foreign keys in React?如何在 React 中插入具有多个外键的表?
【发布时间】:2019-08-22 19:54:56
【问题描述】:

创建一个自行车租赁应用程序并将 addBike() 实现到数据库。 addCustomer() 使用此代码,但我认为这是因为它没有外键。当一半表是外键时如何插入数据库?

谢谢!

newbike.js:

add() {
    bikeService.addBike(
      this.wheelsize,
      this.frame,
      this.gears,
      this.typeId,
      this.location,
      this.orderId,
      id => {
        history.push('/bike/' + id);
        this.props.history.push('/regbike');
      }
    );
  }
}

services.js:

    connection.query(
      'insert into Bike (wheelsize, frame, gears, typeId, statusId, location, orderId) values (?, ?, ?, ?, ?, ?, ?)',
      [wheelsize, frame, gears, typeId, statusId, location, orderId],
      (error, results) => {
        if (error) return console.error(error);

        success(results.insertId);
      }
    );
  }
export let bikeService = new BikeService();

【问题讨论】:

  • 您只需确保已正确设置所有包含外键的列

标签: javascript mysql sql reactjs


【解决方案1】:

您应该在插入之前禁用约束检查并在之后重新启用它(如果您想插入大量数据)

SET FOREIGN_KEY_CHECKS=0;

然后当你完成时

SET FOREIGN_KEY_CHECKS=1;

或者只是插入忽略

"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"

这一切都取决于你想要达到的目标

所以它看起来像这样:

  1. 禁用约束检查。

  2. 将您拥有的所有数据插入到所有相关表中

  3. 启用约束检查。

    a) 确认您没有丢失任何数据。

    b) 验证约束是否实际启用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2013-05-26
    • 2016-10-20
    相关资源
    最近更新 更多