MYSQL: Error: Cannot add or update a child row: a foreign key constraint fails
Posted
by DalivDali
on Stack Overflow
See other posts from Stack Overflow
or by DalivDali
Published on 2010-04-16T00:50:48Z
Indexed on
2010/04/16
0:53 UTC
Read the original article
Hit count: 334
Hi all,
Using MySQL on Windows OS, and am getting an error upon attempting to create a foreign key between two tables:
CREATE TABLE tf_traffic_stats (
domain_name char(100) NOT NULL,
session_count int(11) NULL,
search_count int(11) NULL,
click_count int(11) NULL,
revenue float NULL,
rpm float NULL,
cpc float NULL,
traffic_date date NOT NULL DEFAULT '0000-00-00',
PRIMARY KEY(domain_name,traffic_date))
and
CREATE TABLE td_domain_name (
domain_id int(10) UNSIGNED AUTO_INCREMENT NOT NULL,
domain_name char(100) NOT NULL,
update_date date NOT NULL,
PRIMARY KEY(domain_id))
The following statement gives me the error present in the subject line (cannot add or update a child row: a foreign key constraint fails):
ALTER TABLE td_domain_name
ADD CONSTRAINT FK_domain_name
FOREIGN KEY(domain_name)
REFERENCES tf_traffic_stats(domain_name)
ON DELETE RESTRICT
ON UPDATE RESTRICT
Can someone point me in the right direction of what may be causing the error. I also have a foreign key referencing td_domain_name.domain_id, but I don't think this should be interfering...
Appreciate it!
© Stack Overflow or respective owner