problems with sequelize intermediate table

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 });
  }
};

MODEL:

import { DataTypes } from "sequelize";
import { sequelize } from "../../../database/database.js";
import ORDERS from "../orders/ordersModel.js";
import PDAS from "../equipos/pdas.js";
import CELUS from "../equipos/celus.js";
import PCS from "../equipos/pc.js";
import HARDTICKET from "../equipos/printHard.js";
import POCKET from "../equipos/printPocket.js";
import HANDIES from "../equipos/handies.js";
import COMPLEMENTARY from "../equipos/complementary.js";

const ordersEquipmentModel = sequelize.define(
  "OrdersEquipment",
  {
    orderId: {
      type: DataTypes.INTEGER,
      references: {
        model: ORDERS,
        key: "id",
      },
      allowNull: false,
    },
    celusId: {
      type: DataTypes.INTEGER,
      references: {
        model: CELUS,
        key: "id",
      },
      allowNull: true,
    },
    pdasId: {
      type: DataTypes.INTEGER,
      references: {
        model: PDAS,
        key: "id",
      },
      allowNull: true,
    },
    pcsId: {
      type: DataTypes.INTEGER,
      references: {
        model: PCS,
        key: "id",
      },
      allowNull: true,
    },
    hardTicketId: {
      type: DataTypes.INTEGER,
      references: {
        model: HARDTICKET,
        key: "id",
      },
      allowNull: true,
    },
    pocketId: {
      type: DataTypes.INTEGER,
      references: {
        model: POCKET,
        key: "id",
      },
      allowNull: true,
    },
    handiesId: {
      type: DataTypes.INTEGER,
      references: {
        model: HANDIES,
        key: "id",
      },
      allowNull: true,
    },
    complementaryId: {
      type: DataTypes.INTEGER,
      references: {
        model: COMPLEMENTARY,
        key: "id",
      },
      allowNull: true,
    },
  },
  {
    timestamps: false,
  }
);


export default ordersEquipmentModel;


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.`

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật