Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using Hibernate to create table in mySql database. I have two table "conge" and "employee" and I have a manytoone relationship. the "conge" table have a foreign key of the employee id.

this is my error

Cannot add or update a child row: a foreign key constraint fails (concretepage.Conge, CONSTRAINT FKk1p9i6bbic92cgaad05k4smsg FOREIGN KEY (id) REFERENCES EMPLOYE (id))

Thanks,

I have exported the database this is the sql file
SQL
-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Jul 06, 2017 at 11:50 AM
-- Server version: 10.1.24-MariaDB
-- PHP Version: 7.1.6

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `concretepage`
--

-- --------------------------------------------------------

--
-- Table structure for table `Conge`
--

CREATE TABLE `Conge` (
  `id` int(11) NOT NULL,
  `date` datetime DEFAULT CURRENT_TIMESTAMP,
  `dateDebut` date DEFAULT NULL,
  `dateFin` date DEFAULT NULL,
  `numero` int(11) NOT NULL,
  `status` varchar(255) DEFAULT NULL,
  `employe_id` int(11) DEFAULT NULL,
  `typeConge_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `Conge`
--

INSERT INTO `Conge` (`id`, `date`, `dateDebut`, `dateFin`, `numero`, `status`, `employe_id`, `typeConge_id`) VALUES
(1, '2017-07-06 10:46:30', NULL, NULL, 0, NULL, NULL, NULL),
(2, '2017-07-06 10:46:34', NULL, NULL, 0, NULL, NULL, NULL);

-- --------------------------------------------------------

--
-- Table structure for table `EMPLOYE`
--

CREATE TABLE `EMPLOYE` (
  `id` int(11) NOT NULL,
  `nom` varchar(255) DEFAULT NULL,
  `post_id` int(11) DEFAULT NULL,
  `userInfo_login` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `EMPLOYE`
--

INSERT INTO `EMPLOYE` (`id`, `nom`, `post_id`, `userInfo_login`) VALUES
(1, 'foulen', NULL, 'user'),
(2, 'falten', NULL, 'user');

-- --------------------------------------------------------

--
-- Table structure for table `POST`
--

CREATE TABLE `POST` (
  `id` int(11) NOT NULL,
  `code` varchar(255) DEFAULT NULL,
  `intituler` varchar(255) DEFAULT NULL,
  `employe_id` int(11) DEFAULT NULL,
  `id_superieur` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `TypeConge`
--

CREATE TABLE `TypeConge` (
  `id` int(11) NOT NULL,
  `code` int(11) NOT NULL,
  `intituler` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `TypeConge`
--

INSERT INTO `TypeConge` (`id`, `code`, `intituler`) VALUES
(1, 5757, 'Annuel'),
(2, 2, 'Maladie');

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `login` varchar(255) NOT NULL,
  `country` varchar(255) DEFAULT NULL,
  `enabled` smallint(6) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `role` varchar(255) DEFAULT NULL,
  `employe_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`login`, `country`, `enabled`, `password`, `role`, `employe_id`) VALUES
('admin', NULL, 1, '$2a$10$OlJFlkoM9/nCAK1DUhcE7OvitoDHDip8GuoDt5NrSqWgV5aP7tMeC', 'ROLE_ADMIN', NULL),
('user', NULL, 1, '$2a$10$OlJFlkoM9/nCAK1DUhcE7OvitoDHDip8GuoDt5NrSqWgV5aP7tMeC', 'ROLE_USER', 1);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `Conge`
--
ALTER TABLE `Conge`
  ADD PRIMARY KEY (`id`),
  ADD KEY `FKov9s99mmo220hv4d8ppeobut` (`employe_id`),
  ADD KEY `FKocvumeoahniu5uvsjlwq5mvnp` (`typeConge_id`);

--
-- Indexes for table `EMPLOYE`
--
ALTER TABLE `EMPLOYE`
  ADD PRIMARY KEY (`id`),
  ADD KEY `FKpe9llqbqsni2xqg1vms2h716j` (`post_id`),
  ADD KEY `FKioxxmg7s2j18x2fo7ahnclmcd` (`userInfo_login`);

--
-- Indexes for table `POST`
--
ALTER TABLE `POST`
  ADD PRIMARY KEY (`id`),
  ADD KEY `FKl6st4h0ujkdiun27r7tak7t7n` (`employe_id`),
  ADD KEY `FK3x5ro8omrg46k9jaihue19o8q` (`id_superieur`);

--
-- Indexes for table `TypeConge`
--
ALTER TABLE `TypeConge`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`login`),
  ADD KEY `FK82xfucsr861ymb3t5sp2hulo3` (`employe_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `Conge`
--
ALTER TABLE `Conge`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `EMPLOYE`
--
ALTER TABLE `EMPLOYE`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `POST`
--
ALTER TABLE `POST`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `TypeConge`
--
ALTER TABLE `TypeConge`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- Constraints for dumped tables
--

--
-- Constraints for table `Conge`
--
ALTER TABLE `Conge`
  ADD CONSTRAINT `FKk1p9i6bbic92cgaad05k4smsg` FOREIGN KEY (`id`) REFERENCES `EMPLOYE` (`id`),
  ADD CONSTRAINT `FKlm34pn7fv5rcolnjgjyjnuj9l` FOREIGN KEY (`id`) REFERENCES `TypeConge` (`id`),
  ADD CONSTRAINT `FKocvumeoahniu5uvsjlwq5mvnp` FOREIGN KEY (`typeConge_id`) REFERENCES `TypeConge` (`id`),
  ADD CONSTRAINT `FKov9s99mmo220hv4d8ppeobut` FOREIGN KEY (`employe_id`) REFERENCES `EMPLOYE` (`id`);

--
-- Constraints for table `EMPLOYE`
--
ALTER TABLE `EMPLOYE`
  ADD CONSTRAINT `FKioxxmg7s2j18x2fo7ahnclmcd` FOREIGN KEY (`userInfo_login`) REFERENCES `users` (`login`),
  ADD CONSTRAINT `FKpe9llqbqsni2xqg1vms2h716j` FOREIGN KEY (`post_id`) REFERENCES `POST` (`id`);

--
-- Constraints for table `POST`
--
ALTER TABLE `POST`
  ADD CONSTRAINT `FK3x5ro8omrg46k9jaihue19o8q` FOREIGN KEY (`id_superieur`) REFERENCES `POST` (`id`),
  ADD CONSTRAINT `FKl6st4h0ujkdiun27r7tak7t7n` FOREIGN KEY (`employe_id`) REFERENCES `EMPLOYE` (`id`);

--
-- Constraints for table `users`
--
ALTER TABLE `users`
  ADD CONSTRAINT `FK82xfucsr861ymb3t5sp2hulo3` FOREIGN KEY (`employe_id`) REFERENCES `EMPLOYE` (`id`);
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


What I have tried:

I notice that after inserting exactly two row in my table "conge" I get a foreign key problem. I have used hibernate and also the phpmyadmin to insert and I have always the same problem after inserting two row.
Posted
Updated 7-Jul-17 1:05am
v2
Comments
Suvendu Shekhar Giri 6-Jul-17 6:39am    
"I get a foreign key problem"
Can you share the error details and sample data if possible?
CHill60 6-Jul-17 7:40am    
Share the sql that you are using to insert values into the tables. I've just spotted this in your question. It sounds as if you are inserting values into a table that has a foreign key to another table BEFORE you have entered the data into that other table. You can't do that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900