简体   繁体   English

如何在React中使用多个外键插入表?

[英]How to Insert Into table with multiple foreign keys in React?

Creating a bike rent application and implementing addBike() to database. 创建一个自行车出租应用程序,并将addBike()实现到数据库。 addCustomer() worked with this code, but I think it is because it has no foreign keys. addCustomer()使用此代码,但是我认为这是因为它没有外键。 How do I Insert Into database when half the table is foreign keys? 当一半的表是外键时,如何插入数据库?

Thank you! 谢谢!

newbike.js: 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: 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();

You should disable constraint check before insert and reenable it afterwards (If you want to insert loads of data) 您应该在插入之前禁用约束检查,然后再重新启用约束检查(如果要插入大量数据)

SET FOREIGN_KEY_CHECKS=0;

and then when you're done 然后当你完成

SET FOREIGN_KEY_CHECKS=1;

or just INSERT IGNORE 或只是INSERT IGNORE

"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE" “ INSERT IGNORE”与“ INSERT ... ON DUPLICATE KEY UPDATE”

It all depends on what you want to achieve 这完全取决于您想要实现的目标

So it would look like this: 所以它看起来像这样:

  1. Disable the constraint checks. 禁用约束检查。

  2. Insert all the data you have to all your related tables 将所有数据插入所有相关表

  3. Enable the constraint checks. 启用约束检查。

    a) verify that you didn't loose any data. a)确认您没有丢失任何数据。

    b) verify that the constraints are actually enabled b)验证约束是否已实际启用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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