close

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

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error SQLSTATE[HY000]: General error: 3780 Referencing column ‘user_id’ and referenced column ‘id’ in foreign key are incompatible in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How SQLSTATE[HY000]: General error: 3780 Referencing column ‘user_id’ and referenced column ‘id’ in foreign key are incompatible Error Occurs?

Today I get the following error SQLSTATE[HY000]: General error: 3780 Referencing column ‘user_id’ and referenced column ‘id’ in foreign key are incompatible in php.

How To Solve SQLSTATE[HY000]: General error: 3780 Referencing column ‘user_id’ and referenced column ‘id’ in foreign key are incompatible Error ?

  1. How To Solve SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key are incompatible Error ?

    To Solve SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key are incompatible Error 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

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

    To Solve SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key are incompatible Error 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

Solution 1

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

Solution 2

You can also solve in a more elegant way changing in yout schema of almacen_movimientos you should change:

$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('restrict');

with

$table->foreignId('user_id')->constrained()->onDelete('restrict');

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment