Есть модели
//SmetaCheckupPlan
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class SmetaCheckupPlan extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
SmetaCheckupPlan.belongsTo(models.Smeta, {
foreignKey: 'smetaId',
onDelete: 'CASCADE'
})
}
}
SmetaCheckupPlan.init({
kind: DataTypes.TEXT,
supplier: DataTypes.TEXT,
address: DataTypes.TEXT,
phone:DataTypes.TEXT,
price: DataTypes.TEXT,
}, {
sequelize,
modelName: 'SmetaCheckupPlan',
});
return SmetaCheckupPlan;
};
//CheckupPlan
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class CheckupPlan extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
CheckupPlan.belongsTo(models.Application, {
foreignKey: 'applicationId',
onDelete: 'CASCADE'
})
}
}
CheckupPlan.init({
kind: DataTypes.TEXT,
place: DataTypes.TEXT,
target: DataTypes.TEXT,
supplier: DataTypes.TEXT,
address: DataTypes.TEXT,
phone:DataTypes.TEXT,
price: DataTypes.TEXT,
}, {
sequelize,
modelName: 'CheckupPlan',
});
return CheckupPlan;
};
Их применяю в след функции
async updateappl(req, res, next) {
//обновление заявки (заявка это модель )
try {
const applicationsData = await Application.findOne({where: {id}});
const [smetaData, created] = await Smeta.findOrCreate({
where: {id}, defaults: {
mostProblDiagnosis,
patientName,
patientBirthDate,
patientPromoter,
applicationId: id,
creationDate: new Date().toString(),
execDate: '',
}
});
if (!created) {
await smetaData.update({mostProblDiagnosis, patientName, patientBirthDate, patientPromoter})
}
await CheckupPlan.destroy({where: {applicationId: id}});
await SmetaCheckupPlan.destroy({where: {smetaId: id}});
for (const plan of checkupPlans) {
const result = await CheckupPlan.create({...plan});
const smetaResult = await SmetaCheckupPlan.create({...plan})
await result.setApplication(applicationsData);
await smetaResult.setApplication(smetaData)
}
return res.json(applicationsData);
} catch (e) {
next(e);
}
}
мне нужно при обновлении заявки создать сущность смета и в ее поле SmetaCheckupPlan передать те же данные что и в поле CheckupPlan заявки
у меня выходит ошибка
original: error: duplicate key value violates unique constraint "SmetaCheckupPlans_pkey"
Это потому что я одни те же данные использую ? (.
const result = await CheckupPlan.create({...plan});
const smetaResult = await SmetaCheckupPlan.create({...plan}) )