0

So I have two tables, customers and appointments. Im trying to create a very simple FK relation between appointments.customer_id and customers.id, however when I do my ALTER TABLE trying to add the FK im getting this error:

MySQL said: Cannot add or update a child row: a foreign key constraint fails (wax.#sql-2c5_100, CONSTRAINT customer_fk FOREIGN KEY (id) REFERENCES customers (id) ON DELETE CASCADE ON UPDATE CASCADE)

This constraint "#sql-2c5_100" seems to be some randomly generated constraint, that I can not find ANYWHERE. I've looked on every table on the database, ive looked on all the tables on the information schema and it simply does not exist.

Thanks!

Edit: Here's the create table outputs

CREATE TABLE `appointments` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `customer_id` int(11) unsigned NOT NULL,
  `name` varchar(255) DEFAULT NULL
  PRIMARY KEY (`id`),
  KEY `customer_id` (`customer_id`)
) ENGINE=InnoDB AUTO_INCREMENT=290958 DEFAULT CHARSET=utf8;


CREATE TABLE `customers` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) NOT NULL DEFAULT '',
  `last_name` varchar(255) NOT NULL DEFAULT '',
  `phone` varchar(20) DEFAULT NULL
  PRIMARY KEY (`id`),
  KEY `sf_id` (`sf_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Marina Mosti
  • 461
  • 2
  • 7
  • 1
    Can you add the table information (`SHOW CREATE TABLE` output from the mysql command line, formatted so it is readable pleas) – Norbert Oct 07 '17 at 16:35
  • Possible duplicate of https://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa – Bill Karwin Oct 07 '17 at 16:39
  • Yup, duplicate of https://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa thank you Bill Karwin – Marina Mosti Oct 07 '17 at 18:50
  • HI. Please next time before you ask google your error message with & without suspected DBMS-generated strings, and with & without your own table/column/constraint names. Also 'mysql more info error' with & without 'foreign key'. Also please read the official documentation where it mentions 'foreign key'. – philipxy Oct 11 '17 at 18:33
  • Possible duplicate of [Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails](https://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa) – philipxy Oct 11 '17 at 18:34

0 Answers0