10 days ago I come with this problem I run out of ideas. what happens is that I have a many to many relationship between the entities of orders and equipment that are borrowed. the problem is as follows, I OrdersEquipments model in which I request the id of each team and award them to the order to have a strict control. The problem is that in the forOf cycle when updating the fieldName with their respective model I load a column that should not be because it is not being requested which is pdasId as we will see in the console below:
beforeCreate console:
pcsId VAMOS BIEN OrdersEquipment {
dataValues: { pdasId: null, orderId: 6, pcsId: 1 },
_previousDataValues: { orderId: undefined, pcsId: undefined },
uniqno: 1,
_changed: Set(2) { 'orderId', 'pcsId' },
_options: {
isNewRecord: true,
_schema: null,
_schemaDelimiter: '',
attributes: undefined,
include: undefined,
raw: undefined,
silent: undefined
},
isNewRecord: true
}
CODE JS:
export const createOrder = async (req, res) => {
try {
const {
responsibility,
event,
note,
state,
userId,
pdaCount,
handiesCount,
pcsCount,
printHardCount,
printPocketCount,
celusCount,
complementsCount,
} = req.body;
const selectEquipment = async (model, count) => {
const availableEquipments = await model.findAll({ where: { userId: 1 } });
if (availableEquipments.length < count) {
throw new Error(
`No hay suficientes equipos disponibles de tipo ${model.name}`
);
}
return availableEquipments.slice(0, count);
};
const selectedPDAs = await selectEquipment(PDAS, pdaCount);
const selectedCelus = await selectEquipment(CELUS, celusCount);
const selectedPCs = await selectEquipment(PCS, pcsCount);
const selectedPrintHards = await selectEquipment(
HARDTICKET,
printHardCount
);
const selectedPrintPockets = await selectEquipment(
POCKET,
printPocketCount
);
const selectedHandies = await selectEquipment(HANDIES, handiesCount);
const selectedComplements = await selectEquipment(
COMPLEMENT,
complementsCount
);
if (
selectedPDAs.length === pdaCount &&
selectedCelus.length === celusCount &&
selectedPCs.length === pcsCount &&
selectedPrintHards.length === printHardCount &&
selectedPrintPockets.length === printPocketCount &&
selectedHandies.length === handiesCount &&
selectedComplements.length === complementsCount
) {
const newOrder = await ORDERS.create({
responsibility,
event,
note,
state,
userId,
});
const assignEquipmentsToOrder = async (equipments, model) => {
for (const equipment of equipments) {
console.log(model.name)
const fieldName = `${model.name.toLowerCase()}Id`;
const newOrderEquipment = {
orderId: newOrder.id,
[fieldName]: equipment.id,
};
await ordersEquipmentModel.beforeCreate((newOrderEquipment) => {
if (newOrderEquipment[fieldName] != null) {
console.log(fieldName, "VAMOS BIEN", newOrderEquipment);
} else {
console.log("VAMO MAL", newOrderEquipment);
}
});
await ordersEquipmentModel.create(newOrderEquipment);
equipment.userId = userId;
await equipment.save();
}
};
await assignEquipmentsToOrder(selectedPDAs, PDAS);
await assignEquipmentsToOrder(selectedPCs, PCS);
await assignEquipmentsToOrder(selectedPrintHards, HARDTICKET);
await assignEquipmentsToOrder(selectedPrintPockets, POCKET);
await assignEquipmentsToOrder(selectedCelus, CELUS);
await assignEquipmentsToOrder(selectedHandies, HANDIES);
await assignEquipmentsToOrder(selectedComplements, COMPLEMENT);
return res.status(200).json(newOrder);
} else {
throw new Error("No se pudieron asignar todos los equipos solicitados.");
}
} catch (error) {
return res.status(500).json({ message: error.message });
}
};
I hope you can understand it is the first time I post in a forum, thank you very much!
I tried modifying the model relationships but the error persisted, also the controller logic. The only way I found that it works is when I make the post request and I don’t ask for any entity except the pdas in which the error is the following: “message”: “the null value in the column ‘pdasId’ of the relationship ‘OrdersEquipments’ violates the constraint of not null” and I tried adding the allowNull in the model orderEquipments and it didn’t work either.`