I??m doing migrations in Laravel and this error happens when I proceed with the commandPHP 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 constraintalmacen_movimientos_user_id_foreign
foreign key (user_id
) referencesusers
(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.
Inusers
table you have defined primary key with id as unsigned bigint and inalmacen_movimientos
table the referenceduser_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
You can also solve in a more elegant way changing in yout schema ofalmacen_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');
Have a look at the documentation, you can do it from Laravel 7 on: https://laravel.com/docs/8.x/migrations#foreign-key-constraints
$table->increments('id')
use unsignedInteger in your foreign key
$table->id()
use unsignedBigInteger in your foreign Key
Make sure that foreign key datatype is the same as its referencing column
Follow the dataType to resolve these issues.
public function up(){
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('body');
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
Noticed the local column ('id') was set to bigIncrements('id') and the foreign was set to Unsigned big integer.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.