Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key are incompatible

Tags:

php

mysql

laravel

I´m doing migrations in Laravel and this error happens when I proceed with the command PHP artisan migrate:

In Connection.php line 664:

SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'almacen_movimientos_user_id_foreign' are incompatible. (SQL: alter table almacen_movimientos add constraint almacen_movimientos_user_id_foreign foreign key (user_id) references users (id
) on delete restrict)

In PDOStatement.php line 129:

SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'almacen_movimientos_user_id_foreign' are incompatible.

My migrations look like this:

almacen_movimientos table

public function up()
{
    Schema::create('almacen_movimientos', function (Blueprint $table) {
        $table->unsignedBigInteger('id');
        $table->integer('cliente_proveedor_id');
        $table->integer('empresa_id');
        $table->integer('user_id');
        $table->enum('tipo' , ['ENTRADA' , 'SALIDA' , 'REUBICACION' , 'TRASPASO' , 'DEVOLUCION' , 'MSRO' , 'ENTRADA POR TRASPASO' , 'SALIDA POR TRASPASO'])->nullable();
        $table->string('referencia' , 255)->nullable();
        $table->string('observaciones' , 255)->nullable();
        $table->timestamp('created_at');
        $table->timestamp('updated_at');
        $table->timestamp('deleted_at');
        $table->string('transportista' , 255)->nullable();
        $table->string('operador' , 255)->nullable();
        $table->string('procedencia' , 255)->nullable();
        $table->integer('almacen_id')->nullable();

        $table->foreign('cliente_proveedor_id')->references('id')->on('empresas')->onDelete('restrict');
        $table->foreign('empresa_id')->references('id')->on('empresas')->onDelete('restrict');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('restrict');
        $table->foreign('almacen_id')->references('id')->on('almacenes')->onDelete('restrict');
    });
}

Users Table

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->unsignedBigInteger('id');
        $table->string('name' , 255);
        $table->string('apellido_paterno' , 115)->nullable();
        $table->string('apellido_materno' , 115)->nullable();
        $table->dateTime('fecha_nacimiento')->nullable();
        $table->string('telefono1' , 10)->nullable();
        $table->string('telefono2' , 10)->nullable();
        $table->string('calle' , 255)->nullable();
        $table->string('numero' , 45)->nullable();
        $table->string('colonia' , 255)->nullable();
        $table->string('codigo_postal' , 6)->nullable();
        $table->string('email' , 255)->unique();
        $table->string('user' , 20)->nullable()->unique();
        $table->string('password' , 255);
        $table->string('palabra_secreta' , 255);
        $table->string('remember_token' , 100)->nullable();
        $table->unsignedInteger('empresa_id')->nullable();
        $table->timestamp('created_at');
        $table->timestamp('updated_at');
        $table->timestamp('deleted_at');

        $table->foreign('empresa_id')->references('id')->on('empresas')->onDelete('restrict');
    });
}

Can somebopdy tell me what am I doing wrong? I cannot fix this.

Thank you.

Regards.

like image 975
Rodri6uez Avatar asked Aug 25 '20 19:08

Rodri6uez


People also ask

What is error 3780 in SQL Server?

SQLSTATE [HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'almacen_movimientos_user_id_foreign' are incompatible. (SQL: alter table almacen_movimientos add constraint almacen_movimientos_user_id_foreign foreign key ( user_id) references users ( id

What is error code 3780 in InnoDB?

Error Code: 3780. Referencing column 'personOne' and referenced column 'person' in foreign key constraint 'C' are incompatible. I tried to to set table a engine to InnoDB, but that didn't work.

What is error 3780 (hy000)?

ERROR 3780 (HY000): Referencing column 'bought_by' and referenced column 'id' in foreign key constraint 'product_ibfk_1' are incompatible.

Are referencing columns'personone'and'person'incompatible in InnoDB?

Referencing column 'personOne' and referenced column 'person' in foreign key constraint 'C' are incompatible. I tried to to set table a engine to InnoDB, but that didn't work. I researched the problem more but couldn't figure out how to fix it.


2 Answers

In users table you have defined primary key with id as unsigned bigint and in almacen_movimientos table the referenced user_id is defined as int

change

$table->integer('user_id');

to

$table->unsignedBigInteger('user_id');

The structure and data type of PRIMARY KEY and FOREIGN KEY must be same

like image 109
M Khalid Junaid Avatar answered Oct 03 '22 13:10

M Khalid Junaid


Make sure that foreign key data type is the same as its referencing column

like image 23
Abdelsalam Megahed Avatar answered Oct 03 '22 13:10

Abdelsalam Megahed