Foreign key creation problem

3

I'm having trouble creating a foreign key of these tables:

Code:

ALTER TABLE 'tb_permissao_view' ADD  CONSTRAINT
'fk_tb_permissao_view_tb_sistema_pagina'
FOREIGN KEY ('cd_pagina')
REFERENCES 'tb_sistema_pagina' ('cd_pagina');

Error:

ALTER TABLE 'tb_permissao_view' ADD CONSTRAINT
'fk_tb_permissao_view_tb_sistema_pagina' FOREIGN KEY ('cd_pagina')     
REFERENCES 'tb_sistema_pagina' ('cd_pagina')
Error Code: 1452. Cannot add or update a child row: 
a foreign key constraint fails ('portal'.'#sql-4ee_f', CONSTRAINT 
'fk_tb_permissao_view_tb_sistema_pagina' FOREIGN KEY ('cd_pagina') REFERENCES 
'tb_sistema_pagina' ('cd_pagina')) 
    
asked by anonymous 25.01.2016 / 21:11

1 answer

2

As stated in the comments, the error is triggered because there are values in the cd_pagina column of the tb_permissao_view table that do not exist in the cd_pagina column of the reference table for foreign key tb_sistema_pagina .

All values in the foreign key column must exist in the column to which it is being referenced, and since you said that there is already data in both tables, check that in the cd_pagina column of the tb_permissao_view table there is no data divergences from the table to which it has a foreign key relationship (in this case the tb_sistema_pagina table).

References:

ERROR 1452: Can not add or update child row: a foreign key constraint fails

    
25.01.2016 / 23:21