Update error in relation @Manytoone with Typeorm

Asked

Viewed 26 times

0

Good afternoon, in my application I have the relation that an Order can have several Deliveryrequests. But when I do a save(order) it is updated correctly but an SQL settando is generated as NULL the id_order in the delivery_request table ( in which it is valid and FK).

'UPDATE `tb_delivery_requests` SET `id_order` = NULL WHERE `id_delivery_request` = 17'

In which he shoots the error:

'Cannot add or update a Child Row: a Foreign key Constraint fails (db_atac_moreira.tb_delivery_requests, CONSTRAINT fk_tb_delivery_control_tb_orders1 FOREIGN KEY (id_order) REFERENCES tb_orders (id_order) ON DELETE NO ACTION ON UPDATE NO ACTION)

List of tables below:

Order ts.

class Order {
  @PrimaryGeneratedColumn('increment')
  id_order: number;

  @Column()
  id_shopkeeper: number;

  @ManyToOne(() => Shopkeeper)
  @JoinColumn({ name: 'id_shopkeeper' })
  shopkeeper: Shopkeeper;

  @Column()
  id_seller: number;

  @ManyToOne(() => Seller)
  @JoinColumn({ name: 'id_seller' })
  seller: Seller;

  @Column()
  id_customer: number;

  @ManyToOne(() => Customer)
  @JoinColumn({ name: 'id_customer' })
  customer: Customer;

  @Column()
  order_total_value: number;

  @OneToMany(() => OrderItem, orderItem => orderItem.order)
  items: OrderItem[];

  @OneToMany(() => DeliveryRequest, deliveryRequest => deliveryRequest.order, { cascade: false })
  deliveryRequests: DeliveryRequest[];

  @Column({ type: 'enum', enum: OrderStatus })
  order_status: OrderStatus;

  @CreateDateColumn()
  order_date: Date;

  @Column()
  observation: string;
}

export default Order;

Deliveryrequests.ts

@Entity('tb_delivery_requests')
class DeliveryRequest {
  @PrimaryGeneratedColumn('increment')
  id_delivery_request: number;

  @Column()
  id_shopkeeper: number;

  @ManyToOne(() => Order, { cascade: false })
  @JoinColumn({ name: 'id_order' })
  order: Order;

  @Column({ nullable: true, type: 'numeric' })
  id_driver: number | null;

  @Column({ type: 'enum', enum: DeliveryStatus })
  delivery_status: DeliveryStatus;

  @OneToMany(() => DeliveryRequestItems, deliveryRequestItems => deliveryRequestItems.deliveryRequest, { cascade: false })
  items: DeliveryRequestItems[];

  @CreateDateColumn()
  created_at: Date;

  @Column()
  last_modification: Date;
}

export default DeliveryRequest;

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.