Error trying to insert information into mysql database

Asked

Viewed 68 times

1

I have the following tables

Produto {
  idproduto 
  nome
  preco
}
Promocao {
  descricao
  datainicio
  datatermino
  id_produto_fk
}

When trying to insert a new promotion returns me the following error:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`mercado_db`.`promocao`, CONSTRAINT `produto_id_fk` FOREIGN KEY (`id_produto_fk`) REFERENCES `produto` (`idproduto`) ON DELETE NO ACTION ON UPDATE NO ACTION)

1 answer

0

You are trying to insert a promotion for a product that does not exist. You have already taken a look at your insert promotion, the value passed to the field id_produto_fk is a value that exists in the table produto.idproduto?

  • Guy did some testing here, I have an app that sends a Promotion object to the method insert with the above data. In the app I print the promotion.id_producto_fk and appears the product id in the component but before sending to the bank in the insert method I put a print of the object to save and in id_producto_fk appears 0, I do not know what else to do to solve this.

Browser other questions tagged

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