我想知道当用户向后请求新数据时如何生成主键和外键。
这是我所拥有的:
Terms.create = (req) => {
let userId = req.params.UserId;
let yearId = req.params.YearId;
let created = {
termTitle: req.body.termTitle,
termStart: req.body.termStart,
termEnd: req.body.termEnd,
termRotation: req.body.termRotation
}
return database.query(
`INSERT INTO Terms
(UserId, YearId, TermTitle, TermStart, TermEnd, TermRotation)
VALUES (${userId}, ${yearId}, ${created.termTitle},
${created.termStart}, ${created.termEnd}, ${created.termRotation})`
);
}
此处,userId和yearId是外键。我这样做正确/有效吗?我假设为了使其正常工作,当用户向表单提出GET请求时,我需要在url参数中包括两个外键ID,并且termId(PK)将自动生成(我已经附加了auto_increment在MySQL表中查看“条款”。