ERROR CODE 1215 - You can not add the foreign key

0

Table 1:

Table2:

Command: ALTER TABLE extras ADD FOREIGN KEY (login) REFERENCES usuario(login);

Response: Error Code: 1215. Cannot add foreign key constraint

  

Can not find an index in the referenced table where the   Referenced columns appear as the first columns, or the types   columns in the table and the referenced table do not match the   restriction. Note that the internal storage type of ENUM and SET   has changed in

I can not identify the reason for the error. Can anyone help me?

Ps. as you can see, the two fields are of the same type (varchar (15))

Thank you so much

    
asked by anonymous 11.12.2017 / 17:51

1 answer

1

It seems that its structure is correct, check if the table type is like InnoDB itself, they can be like MyISAM which does not allow FK creation, follow script that I created from your print and worked normally:

CREATE TABLE IF NOT EXISTS 'usuario' (
  'login' VARCHAR(15) NOT NULL,
  'nome' VARCHAR(40) NOT NULL,
  'senha' VARCHAR(10) NOT NULL,
  PRIMARY KEY ('login'))
ENGINE = InnoDB;


CREATE TABLE IF NOT EXISTS 'extras' (
  'idFunc' INT NULL,
  'login' VARCHAR(15) NOT NULL,
  'data' DATETIME NULL,
  'extra' TEXT NULL,
  INDEX 'fk_extras_usuario_idx' ('login' ASC),
  CONSTRAINT 'fk_extras_usuario'
    FOREIGN KEY ('login')
    REFERENCES 'usuario' ('login')
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;
    
11.12.2017 / 19:34