Error Code: 1215. Can not add foreign key constraint. MySQL problem

0

I'm having an error in the creation of a table, I'm getting the error of MySQL 1215

  • Already check the data types

  • The name is the same

The creation of the users table is the following:

create table if not exists usuarios ( -- listo
	idusuarios INT not null auto_increment,
    documento INT not null,
    nombre varchar(45),
    apellido varchar(45),
    email varchar(45),
    observaciones varchar(90),
    natalicio varchar(45),
    estado char(1),
    domicilio varchar(45),
    primary key (idusuarios,documento)
    )
engine=innodb;

The creation of the charges table is as follows:

create table cargos(
	idCargos int not null auto_increment primary key,
    nombre varchar(30) not null,
    estado char(1),
    descripcion varchar(45),
    observaciones varchar(90) null default null
)engine=innodb;

And the creation of the employeesCargos table is as follows:

create table if not exists empleadosCargos(
	Documento INT not null,
    idCargos INT not null,
    idUsuarios int not null,
    foreign key (Documento,idUsuarios) references usuarios(documento,idusuarios),
    foreign key (idCargos) references cargos(idcargos)
)engine=InnoDB;

This is the image of the error:

Thanks in advance. Greetings!

    
asked by Chehin 09.06.2018 в 01:01
source

1 answer

2

I really do not know the cause of the problem but do a test as follows:

create table if not exists empleadosCargos( 
     Documento INT not null, 
     idCargos INT not null, 
     idUsuarios int not null, 
     foreign key (idUsuarios, Documento) references usuarios(idusuarios, documento), 
     foreign key (idCargos) references cargos(idcargos) 
)engine=InnoDB 

Create the table without problems, so I guess that the order of the columns was affecting the creation of the table because it does not work in the following way:

foreign key (Documento,idUsuarios) references usuarios(documento,idusuarios)

But as follows if you create the table:

foreign key (idUsuarios, Documento) references usuarios(idusuarios, documento)
    
answered by 09.06.2018 в 01:19