私は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);
}
};
1x患者エントリと1xpersonalInformationエントリの代わりに、結果は2x患者エントリ1xpersonalInformationになります。personalInformationエントリには、テーブルに患者のuuidがあります(作成された2人の患者の後者)。なぜ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;
}
はい、2回作成しているため、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,
}]
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加