Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

449
Views
SQLSTATE [HY000]: error general: 3780 La columna de referencia 'user_id' y la columna de referencia 'id' en la clave externa son incompatibles

Estoy haciendo migraciones en Laravel y este error ocurre cuando procedo con el comando PHP artisan migrate :

En Connection.php línea 664:

SQLSTATE[HY000]: Error general: 3780 La columna de referencia 'user_id' y la columna referenciada 'id' en la restricción de clave externa 'almacen_movimientos_user_id_foreign' son incompatibles. (SQL: alterar tabla almacen_movimientos añadir restricción almacen_movimientos_user_id_foreign clave foránea ( user_id ) referencias users ( id
) en eliminar restringir)

En PDOStatement.php línea 129:

SQLSTATE[HY000]: Error general: 3780 La columna de referencia 'user_id' y la columna referenciada 'id' en la restricción de clave externa 'almacen_movimientos_user_id_foreign' son incompatibles.

Mis migraciones se ven así:

tabla almacen_movimientos

 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'); }); }

Tabla de usuarios

 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'); }); }

¿Alguien puede decirme qué estoy haciendo mal? No puedo arreglar esto.

Gracias.

Saludos.

over 4 years ago · Santiago Trujillo
5 answers
Answer question

0

En la tabla de users , ha definido la clave principal con id como bigint sin firmar y en la tabla almacen_movimientos , el user_id al que se hace referencia se define como int

cambiar

 $table->integer('user_id');

para

 $table->unsignedBigInteger('user_id');

La estructura y el tipo de datos de PRIMARY KEY y FOREIGN KEY deben ser iguales

over 4 years ago · Santiago Trujillo Report

0

Asegúrese de que el tipo de datos de clave externa sea el mismo que su columna de referencia

over 4 years ago · Santiago Trujillo Report

0

También puedes resolver de una forma más elegante cambiando en tu esquema de almacen_movimientos debes cambiar:

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

con

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

Eche un vistazo a la documentación, puede hacerlo desde Laravel 7 en:https://laravel.com/docs/8.x/migrations#foreign-key-constraints

over 4 years ago · Santiago Trujillo Report

0

Siga el tipo de datos para resolver estos problemas.

 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'); }); }

Notó que la columna local ('id') se estableció en bigIncrements('id') y la externa se estableció en Entero grande sin signo.

over 4 years ago · Santiago Trujillo Report

0

$table->increments('id')

use unsignedInteger en su clave externa

 $table->id()

use unsignedBigInteger en su clave externa

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!