ERROR 1215: Can not add foreign key constraint (MySQL Workbench)

0

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

ALTER TABLE 'acoes_db'.'Carteira' 
ADD CONSTRAINT 'carteira_fk'
  FOREIGN KEY ('ID')
  REFERENCES 'acoes_db'.'Acionistas' ('Carteira')
  ON DELETE CASCADE
  ON UPDATE CASCADE;
    
asked by anonymous 18.04.2018 / 08:41

1 answer

1

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

You are trying to add FK in the parent reference of the daughter table (the opposite scenario).

Try to do this:

ALTER TABLE 'acoes_db'.'Acionistas' 
    ADD CONSTRAINT 'carteira_fk'
    FOREIGN KEY ('Carteira')
    REFERENCES 'acoes_db'.'Carteira' ('ID');
    
18.04.2018 / 12:18