Mysql database on phpmyadmin, foreign key

This edit will be visible only to you until it is peer reviewed.
i have a probleme with creation foreight key in phpmy admin so, i join my database and sql qource witch i use it,

ALTER TABLE departments ADD CONSTRAINT contrainte_fk FOREIGN KEY (company_fk) REFERENCES companies(company_id) ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE `branches` (
  `branch_id` int(11) NOT NULL,
  `company_fk` int(11) NOT NULL,
  `department_fk` int(11) NOT NULL,
  `branch_name` varchar(100) DEFAULT NULL,
  `branch_adress` varchar(255) NOT NULL,
  `branch_created_date` date NOT NULL,
  `branch_status` enum('active','inactive') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

--
-- Structure de la table `companies`
--

CREATE TABLE `companies` (
  `company_id` int(11) NOT NULL DEFAULT '0',
  `company_name` varchar(100) NOT NULL,
  `company_email` varchar(100) NOT NULL,
  `company_adress` varchar(255) NOT NULL,
  `company_created_date` date NOT NULL,
  `company_status` enum('active','inactive') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

--
-- Structure de la table `departments`
--

CREATE TABLE `departments` (
  `department_id` int(11) NOT NULL,
  `company_fk` int(11) NOT NULL,
  `department_name` varchar(100) NOT NULL,
  `department_created_date` date NOT NULL,
  `department_status` enum('active','inactive') NOT NULL,
  `company_name` varchar(100) NOT NULL,
  `branch_name` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

What problem?

ALTER TABLE branches ADD FOREIGN KEY (company_fk) REFERENCES companies(company_id) ON DELETE RESTRICT ON UPDATE CASCADE;

MySQL a répondu:

#1452 - Cannot add or update a child row: a foreign key constraint
 fails (`advanced_yii2`.`#sql-15d0_4c5`, CONSTRAINT 
`#sql-15d0_4c5_ibfk_1` FOREIGN KEY (`company_fk`) REFERENCES `companies`
 (`company_id`) ON UPDATE CASCADE)

ErreurRequête SQL :

ALTER TABLE branches ADD FOREIGN KEY (department_fk) REFERENCES departments(department_id) ON DELETE RESTRICT ON UPDATE RESTRICT;

MySQL a répondu:

#1452 - Cannot add or update a child row: a foreign key constraint
 fails (`advanced_yii2`.`#sql-15d0_4c5`, CONSTRAINT 
`#sql-15d0_4c5_ibfk_1` FOREIGN KEY (`department_fk`) REFERENCES 
`departments` (`department_id`))

and also

Requête SQL :

ALTER TABLE departments ADD FOREIGN KEY (company_fk) REFERENCES companies(company_id) ON DELETE CASCADE ON UPDATE CASCADE;

MySQL a répondu:

#1452 - Cannot add or update a child row: a foreign key constraint
 fails (`advanced_yii2`.`#sql-15d0_4b6`, CONSTRAINT 
`#sql-15d0_4b6_ibfk_1` FOREIGN KEY (`company_fk`) REFERENCES `companies`
 (`company_id`) ON DELETE CASCADE ON UPDATE CASCADE)

Are the tables empty?
If they already contain data, the data has to be consistent with the foreign key rules you want to add, otherwise you’ll get these “foreign key contraint fails” errors. It probably means that the value is missing in the table you’re referencing to.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.