As a newcomer to Vue.js and Express, I've been trying to figure out how to transfer the GuestID
from the Guest
Table to the foreign key GuestID
in my reservations
table.
app.post('/create',(req,res,next)=>{
var mysql = require('mysql')
var connection = mysql.createConnection(config)
var sql = "INSERT INTO `guest` SET ?"
connection.query(sql,req.body,(err,results,fields)=>{
connection.end()
if(err){
next(err)
}else{
res.json([true,results.insertId])
}
})
})
app.post('/book',(req,res,next)=>{
var mysql = require('mysql')
var connection = mysql.createConnection(config)
var sql = "INSERT INTO `reservation` SET `GuestID`=LAST_INSERT_ID(),`RoomID`=?,`AdultCount`=?,`KidCount`=?,`CheckInDate`=?,`CheckOutDate`=?"
var RoomID = req.body.RoomID;
var AdultCount = req.body.AdultCount;
var KidCount = req.body.KidCount;
var CheckInDate = req.body.CheckInDate;
var CheckOutDate = req.body.CheckOutDate;
connection.query(sql,[RoomID,AdultCount,KidCount,CheckInDate,CheckOutDate],(err,results,fields)=>{
connection.end()
if(err){
next(err)
}else{
res.json([true,results])
}
})
})
I'm encountering this specific Error message:
Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (
booking
.reservation
, CONSTRAINTreservation_ibfk_1
FOREIGN KEY (GuestID
) REFERENCESguest
(GuestID
))