I have a parent orders
table like this:
+---------------+---------------+------+-----+-----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+-----------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| pnref | varchar(50) | YES | UNI | NULL | |
| customerid | int(11) | YES | | NULL | |
+-------------------------------------------------------------------------+
I have a child orders_content
table like this:
+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| id | int(6) | NO | PRI | NULL | auto_increment |
| orderid | int(11) | NO | | NULL | |
| status | varchar(50) | YES | | pending | |
+------------------+--------------+------+-----+---------+----------------+
I am attempting to add aFOREIGN KEY
like this:
ALTER TABLE orders_content
ADD CONSTRAINT fk_orders_content
FOREIGN KEY(orderid) REFERENCES orders(id)
ON DELETE CASCADE ON UPDATE CASCADE;
I cannot resolve why this fails:
1452 - Cannot add or update a child row: a foreign key constraint fails (
gls.
#sql-1744_4d58, CONSTRAINT
fk_orders_contentFOREIGN KEY (
orderid) REFERENCES
orders(
id) ON DELETE CASCADE ON UPDATE CASCADE)
If anyone can see my error or errors I would appreciate it.