#1452 - Cannot add or update a Child Row: a Foreign key Constraint fails

Asked

Viewed 223 times

0

Personal talk I am having problems to create a foreign key in another table... You are giving this error:

#1452 - Cannot add or update a child row: a foreign key constraint fails....

Follow the command I’m doing to create foreign key:

ALTER TABLE cad_cadastro_cliente_acb ADD FOREIGN KEY (ID_SERV_CLIENTE_FK) REFERENCES cad_fluxo_servico(ID_CAD_FLUXO_SERVICO)

Follow the tables I created in the database:

CREATE TABLE cad_cadastro_cliente_acb(
ID_CLIENTE_ACB int(10) AUTO_INCREMENT, 
TIPO_PJ_PF_CLIENTE_ACB varchar(50), 
NOME_RAZAO_SOCIAL_CLIENTE_ACB varchar(100), 
CNPJ_CLIENTE_ACB varchar(50), 
CPF_CAD_ACB varchar(50), 
NOME_CONTATO_CLIENTE_ACB varchar(100), 
NOME_SOCIAL_CLIENTE_ACB varchar(100),
COMO_CONHECEU_CLIENTE_ACB varchar(10),   
ENDERECO_CLIENTE_ACB varchar(100), 
COMPLEMENTO_CLIENTE_ACB varchar(100), 
BAIRRO_CLIENTE_ACB varchar(100), 
CIDADE_CLIENTE_ACB varchar(100), 
CEP_CLIENTE_ACB varchar(100), 
UF_CLIENTE_ACB varchar(100), 
EMAIL_CLIENTE_ACB varchar(50), 
CONTATO01_CLIENTE_ACB varchar(15), 
CONTATO02_CLIENTE_ACB varchar(15), 
OBS_CLIENTE_ACB varchar(300),
ID_SERV_CLIENTE_FK INT(10) NOT NULL, 
USER_CAD_LOG varchar(20),
DATA_CAD_USER datetime,
PRIMARY KEY (ID_CLIENTE_ACB)
)ENGINE = innodb;



CREATE TABLE cad_fluxo_servico(
ID_CAD_FLUXO_SERVICO int(10) AUTO_INCREMENT,
SETOR_SERVICO varchar(20),
TITULO_SERVICO VARCHAR (100),
DESCRICAO_SERVICO VARCHAR(500),
RESPONSAVEL_SERVICO VARCHAR (50),
USUARIO_CAD varchar (50),
DATA_CAD_USER datetime,
PRIMARY KEY (ID_CAD_FLUXO_SERVICO)
)ENGINE = innodb;
  • Query whether all values in cad_cadastro_cliente_acb.ID_SERV_CLIENTE_FK effectively exist in cad_fluxo_servico.ID_CAD_FLUXO_SERVICO. The error may be due to an improper reference.

  • Try SELECT cad_cadastro_cliente_acb.ID_CLIENTE_ACB, cad_cadastro_cliente_acb.ID_SERV_CLIENTE_FK 
FROM cad_cadastro_cliente_acb LEFT OUTER JOIN cad_fluxo_servico ON (cad_cadastro_cliente_acb.ID_SERV_CLIENTE_FK = cad_fluxo_servico.ID_CAD_FLUXO_SERVICO)
WHERE cad_fluxo_servico.ID_CAD_FLUXO_SERVICO IS NULL

  • The friend returned a value a value yes... BUT the table cad_fluxo_servico is empty

  • 1

    If you returned a value then this value has to exist in the table cad_fluxo_servico so that the foreign key can be created. You can clear the table cad_cadastro_cliente_acb create the foreign key and then enter the data.

  • worked... thanks my friend... I had to clear the table

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.