我有2个模型,patient
和personalInformation
。
存在一个与personalInformation.belongsTo(patient)
的关联。就我而言,patient
主键或uuid在personalInformation
表中作为一列存在。
当我的API收到一个POST
请求时,我打算让我的控制器一起创建一个patient
条目和一个personalInformation
条目。
这是我的控制器代码:
// patient.controller.js
const db = require('../../db/databaseConfig');
const Patient = db.patients;
const PersonalInformation = db.personalInformation;
PersonalInformation.Patient = PersonalInformation.belongsTo(Patient);
exports.create = async (req, res) => {
try {
let payload = await req.body;
await console.log('Recevied request: CREATE /api/patients & payload: ', payload);
let newPatientEntry = Patient.create();
let newPInfo = await PersonalInformation.create({
...payload,
patient: newPatientEntry
}, {
include: [{
association: PersonalInformation.Patient,
}]
});
res.send(newPInfo);
} catch (e) {
console.log('error inside create method Patient controller');
throw new Error(e);
}
};
结果不是2个患者条目和1个personalInformation条目,而是1个患者输入和1x个人信息条目。 personalInformation条目在表中具有患者的uuid(在创建的2位患者中,后者是后者)。我不知道为什么要输入两个病人。
这是相关的nodejs日志:
2018-12-05T01:17:57.644499+00:00 app[web.1]: Executing (default): INSERT INTO "patients" ("uuid","createdAt","updatedAt") VALUES ('89290b66-999d-4f10-96b3-4105a26a9450','2018-12-05 01:17:57.583 +00:00','2018-12-05 01:17:57.583 +00:00') RETURNING *;
2018-12-05T01:17:57.645703+00:00 app[web.1]: Executing (default): INSERT INTO "patients" ("uuid","createdAt","updatedAt") VALUES ('9ed075da-757a-457b-b939-ed7e4c7b523a','2018-12-05 01:17:57.612 +00:00','2018-12-05 01:17:57.612 +00:00') RETURNING *;
2018-12-05T01:17:57.659918+00:00 app[web.1]: Executing (default): INSERT INTO "personalInformations" ("id","nameTitle","nameFirst","nameMiddle","nameLast","nameSuffix","nameAlias","dateOfBirth","sex","gender","occupation","deceased","createdAt","updatedAt","patientUuid") VALUES (DEFAULT,'Mr','Robert','Swan','Mueller','II','Bob','1950-12-15 00:00:00.000 +00:00','Male','Man','Special Counsel','False','2018-12-05 01:17:57.594 +00:00','2018-12-05 01:17:57.594 +00:00','9ed075da-757a-457b-b939-ed7e4c7b523a') RETURNING *;
模型的结构如下:
// patient.model.js
const uuid = require('uuid/v4');
module.exports = (sequelize, Sequelize) => {
const Patient = sequelize.define('patient', {
uuid: {
primaryKey: true,
allowNull: false,
type: Sequelize.UUID,
defaultValue: () => uuid(),
}
});
return Patient;
};
和
// personalInformation.model.js
module.exports = (sequelize, Sequelize) => {
const PersonalInformation = sequelize.define('personalInformation', {
nameFirst: {
type: Sequelize.STRING,
notEmpty: true,
allowNull: false,
},
nameLast: {
type: Sequelize.STRING,
notEmpty: true,
allowNull: false,
},
... etc
... etc
});
return PersonalInformation;
}
答案 0 :(得分:0)
是的,它将创建2个条目,因为您创建了两次:
let newPatientEntry = Patient.create(); // <----- HERE
let newPInfo = await PersonalInformation.create({
...payload,
patient: newPatientEntry // <----- HERE
}, {
include: [{
association: PersonalInformation.Patient,
}]
});
您可以尝试以下操作:
// let newPatientEntry = Patient.create(); // <----- REMOVE THIS
let newPInfo = await PersonalInformation.create({
...payload,
patient: {} //<---- MAKE THIS BLANK
}, {
include: [{
association: PersonalInformation.Patient,
}]
});