SQL FK constraint fails

64 views Asked by At

I have 2 tables, Office and User. I want to make relation OneToMany (1 office has many users).

But when i run this sql

ALTER TABLE izo_user ADD CONSTRAINT FK_DA8075CFFA0C224 FOREIGN KEY (office_id) REFERENCES izo_office (id)
CREATE INDEX IDX_DA8075CFFA0C224 ON izo_user (office_id)

something goes wrong and i get error

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`izoplast`.`#sql-9842_1be9`, CONSTRAINT `FK_DA8075CFFA0C224` FOREIGN KEY (`office_id`) REFERENCES `izo_office` (`id`))

My Tables: http://oi57.tinypic.com/whhezr.jpg

1

There are 1 answers

0
Zafar Malik On

try it if it works for you-

ALTER TABLE izo_user add index idx_office_id(office_id);
ALTER TABLE izo_user ADD CONSTRAINT FK_DA8075CFFA0C224 FOREIGN KEY (office_id) REFERENCES izo_office (id);