创建自行车租赁应用程序并将 addBike() 实现到数据库。 addCustomer() 使用此代码,但我认为这是因为它没有外键。当表的一半是外键时,如何插入数据库?
谢谢!
新自行车.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');
}
);
}
}
服务.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();
最佳答案
你应该在插入前禁用约束检查并在插入后重新启用它(如果你想插入大量数据)
SET FOREIGN_KEY_CHECKS=0;
然后当你完成的时候
SET FOREIGN_KEY_CHECKS=1;
或者只是插入忽略
"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"
一切都取决于你想达到什么目的
所以它看起来像这样:
禁用约束检查。
将您拥有的所有数据插入所有相关表
启用约束检查。
a) 确认您没有丢失任何数据。
b) 验证约束是否实际启用
关于javascript - 如何在 React 中插入具有多个外键的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55458684/