Problem creating Foreign key in mysql

Asked

Viewed 450 times

0

Well I have to relate 3 tables and I’m trying to create the foreign key I can create only that in php I have a INSERT for one of the tables that does not perform the insertion and accuses the following what I am doing wrong in the Foreign key ? How can I solve this problem ?

Error

foreign key constraint fails (`sabeonde_sabeonde`.`notificacoes_visualizacao`,
    CONSTRAINT `fk_notificacao_visualizacao_notificacao` 
    FOREIGN KEY (`id_notificacao`) REFERENCES `notificacoes` (`id`) ON DELETE
    NO ACTION ON UPD)' in  /home/sabeonde/public_html/ajax/mostra_notificacoes.php:41
Stack trace:
#0 /home/sabeonde/public_html/ajax/mostra_notificacoes.php(41): PDOStatement->execute()
#1 {main}

Notifications

CREATE TABLE IF NOT EXISTS `notificacoes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`user_destino_notificacao` int(11) NOT NULL,
`notificacao` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`tipo` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;

Table notifications display

  CREATE TABLE IF NOT EXISTS `notificacoes_visualizacao` (
  `id_notificacao` int(10) NOT NULL,
  `id_usuario_que_visualizou` int(10) NOT NULL,
  PRIMARY KEY (`id_notificacao`,`id_usuario_que_visualizou`),
  KEY `fk_notificacao_visualizacao_usuario_idx` (`id_usuario_que_visualizou`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Constraints for dumped tables
--

--
-- Limitadores para a tabela `notificacoes_visualizacao`
--
ALTER TABLE `notificacoes_visualizacao`
  ADD CONSTRAINT `fk_notificacao_visualizacao_notificacao` FOREIGN KEY (`id_notificacao`) REFERENCES `notificacoes` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_notificacao_visualizacao_usuario` FOREIGN KEY   (`id_usuario_que_visualizou`) REFERENCES `users_social` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE;

Insert php code

    $sql = $conexao->prepare("INSERT INTO notificacoes_visualizacao (id_notificacao, id_usuario_que_visualizou) VALUES (:id_notificacao, :id_usuario_que_visualizou)");
    $sql->bindParam(':id_notificacao', $row_notificacoes['id'], PDO::PARAM_INT);
    $sql->bindParam(':id_usuario_que_visualizou', $user_actual, PDO::PARAM_INT); 
    $sql->execute();
  • If possible, add the php input code to your question.

  • I already edited the question and put the code

No answers

Browser other questions tagged

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