I have two tables named portfolio and portfolioplants. A one portfolio could be have multiple plants. When user create new portfolio with plants from API ı want to hold the portfolioID(PrimaryKey) and portfolio name in portfolio table, and hold the plants of portfolio's in portfolioplants table with portfolioID(ForeignKey) and plants of portfolio. What I'd like to do is, ı want to save portfolioID value (which is auto-incremented in portfolios table as a PK) in the portfolioID(FK) column of portfolioplants table automaticcly. So here is my sql querys:
app.post('/api/savePortfolio',(req,res)=>{
const portfolioName = req.body.name
const plants = req.body.plants
const sqlInsertportfolio = "INSERT INTO portfolios (name) VALUES (?)"
db.query(sqlInsertportfolio,[portfolioName],(err,result)=>{
if(err) console.log(err)
console.log(result)
res.status(200)
});
const sqlInsertPortfoliofPlants = "INSERT INTO portfolioplants (plantName) VALUES (?)"
plants.forEach(plant=>{
db.query(sqlInsertPortfoliofPlants,[plant.name],(err,result)=>{
if (err) console.log(err)
console.log(result)
res.status(200)
})
})
})
here is the portfolios table:
and here is the portfolioplants table:
I'd like the portfolioID column to be filled with the auto generated portfolioID value of its portfolio. THX in advance for your suggestions and have a healty day :))