Search code examples
nestjstypeorm

NestJS TypeORM Post Request for a Has Many Through Type Relationship


I'm attempting to implement a many to many relationship with a custom field exactly like this illustrates. My data is different than the example he gives, however the structure of the relationship is identical. I am able to successfully create the relationships, but I'm having an issue where the foreign key for product (product_id) is null even though the client is sending data for that field. For some reason TypeORM is dropping it and inputting a null value.

Here is the structure of the junction table:

@Entity()
export class ShipmentProduct {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  qty: number;

  @Column()
  pick_up: boolean;

  @ManyToOne(type => Shipment, shipment => shipment.shipmentProducts)
  @JoinColumn({ name: 'shipment_id' })
  shipment: Shipment;

  @ManyToOne(type => Product, product => product.shipmentProducts)
  @JoinColumn({ name: 'product_id' })
  product: Product;

}

My Service submits the data by the following function:

 async create(shipmentData: CreateShipmentDto): Promise<Shipment> {
   const shipment = new Shipment();
   shipment.bill_of_lading = shipmentData.bill_of_lading;
   shipment.trailer_number = shipmentData.trailer_number;
   shipment.ship_date = shipmentData.ship_date;
   shipment.delivery_date = shipmentData.delivery_date;
   shipment.carrier = shipmentData.carrier;
   shipment.release_code = shipmentData.release_code;
   shipment.payment_date = shipmentData.payment_date;
   shipment.comments = shipmentData.comments;
   shipment.client = await this.clientsRepository.findOne(shipmentData.client_id);
   shipment.shipmentProducts = shipmentData.shipmentProducts;

   return await this.shipmentsRepository.save(shipment);
 }

when submitting the form, shipment data is saved successfully along with shipmentProducts, however the product_id is dropped even though shipmentData contains a value for product_id.

This is a console.log of shipmentData

{
  client_id: 1,
  bill_of_lading: '12',
  trailer_number: '21',
  ship_date: '2020-04-02T04:00:00.000Z',
  delivery_date: '',
  carrier: '21',
  release_code: '21',
  fuel_surcharge: '21',
  payment_date: '',
  comments: '',
  shipmentProducts: [
    { product_id: 1966, qty: '12', pick_up: false },
    { product_id: 1966, qty: '12', pick_up: false }
  ]
}

However, the insert statement for shipmentProducts omits product_id and insists on a default value. Why? How do I fix it?

INSERT INTO `shipment_product`(`id`, `qty`, `pick_up`, `shipment_id`, `product_id`) VALUES (DEFAULT, ?, ?, ?, DEFAULT) -- PARAMETERS: ["12",0,10]

Solution

  • Turns out I needed to send at least a partial product object in my post request and not just product_id. So this seemed to do the trick. Problem was with the client side and not NestJS or TypeORM.

    shipmentProducts: [
      { product: { id:1966 }, qty: 12, pick_up: false }
      { product: { id:1845 }, qty: 20, pick_up: false }
    ]