ERROR 1215: Cannot add Foreign key Constraint (Mysql Workbench)

Asked

Viewed 811 times

0

Hello I am trying to add a foreign key to a table but I get error 1215. I have already checked the attributes and all are compatible, the portfolio field is a primary key, I do not understand why it is not possible to add the key.

Tabela Acionistas

Tabela Carteira

Inserção da FK

inserir a descrição da imagem aqui

ALTER TABLE `acoes_db`.`Carteira` 
ADD CONSTRAINT `carteira_fk`
  FOREIGN KEY (`ID`)
  REFERENCES `acoes_db`.`Acionistas` (`Carteira`)
  ON DELETE CASCADE
  ON UPDATE CASCADE;

1 answer

1

As its name suggests Foreign Key (foreign key) is the column(s) that identifies(m) the parent in from the child table.

You are trying to add the FK in the father’s reference to the daughter table (the opposite scenario).

Try to do it this way:

ALTER TABLE `acoes_db`.`Acionistas` 
    ADD CONSTRAINT `carteira_fk`
    FOREIGN KEY (`Carteira`)
    REFERENCES `acoes_db`.`Carteira` (`ID`);

Browser other questions tagged

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