I'm trying to add a foreign key to this table with the following code block
public function up()
{
Schema::create('registros', function (Blueprint $table) {
$table->increments('id');
$table->integer( 'cliente' )->unsigned()->index();
$table->integer( 'item' )->unsigned()->index();
$table->decimal( 'vl_preco',10,2 );
$table->char( 'sn_pago',1 );
$table->integer( 'qt_compra' )->unsigned();
$table->foreign( 'cliente' )->references( 'clientes' )->on('id') ;
$table->foreign( 'item' )->references( 'item' )->on('id') ;
$table->timestamps();
});
}
But I'm getting the following message:
[Illuminate \ Database \ QueryException] SQLSTATE [HY000]: General error: 1215 Can not add foreign key constraint (SQL: alter table
registros
add constraintregistros_cliente_foreign
foreign key (cliente
) referencesid
(clientes
))[PDOException] SQLSTATE [HY000]: General error: 1215 Can not add foreign key constraint
The tables that I'm trying to associate exist with and with the respective primary keys autoincrement.