0

How can I prevent duplication of records with the same combinations of entities, here : id_product and id_customer. When I click "save relation" a relation (many-to-many) between product and customer is created and this relation has its own id, id_product and id_customer. Is there any solution to block creation of relation between product and customer if such combination already exists in MySQL database ?

public saveRelation = (relationFormValue) => {
const newRelation = {
  id_product: relationFormValue.id_product ,
  id_customer: relationFormValue.id_customer
};

const dialogRef = this.dialog.open(ConfirmDialogComponent, {
  maxWidth: "400px",
  data: new ConfirmDialogModel("Please confirm",'Are you sure to save this relation ?')
});

dialogRef.afterClosed().subscribe(dialogResult => {
  if (dialogResult==true) {
    this.relationService.create(newRelation)
      .subscribe(
        response => {
          this.dialogRef.close(true);              
        },
        error => {
          this.errorService.handleError(error);
        });
  }
});

}

  • 1
    If you don't have the information about existing relations on frontend, you cannot block it there. In that case, you have to do it on backend. Take a look at `INSERT IGNORE INTO table` https://stackoverflow.com/a/1361368/501936 – Marek Augustyn Jul 15 '20 at 08:43
  • Thank you, I have already asked related question concerning this issue on the backend side with node.js and MySQL : https://stackoverflow.com/questions/62910689/how-to-block-insert-duplicate-record-in-node-mysql-for-many-to-many-relation – anna118690 Jul 15 '20 at 08:49

0 Answers0