【发布时间】:2020-12-14 12:56:25
【问题描述】:
我正在 Laravel 中进行迁移,当我继续执行命令 PHP artisan migrate 时会发生此错误:
在 Connection.php 第 664 行:
SQLSTATE[HY000]:一般错误:3780 在外键约束“almacen_movimientos_user_id_foreign”中引用列“user_id”和引用列“id”不兼容。 (SQL:alter table
almacen_movimientos添加约束almacen_movimientos_user_id_foreign外键(user_id)引用users(id
) 删除限制)
在 PDOStatement.php 第 129 行:
SQLSTATE[HY000]:一般错误:3780 在外键约束“almacen_movimientos_user_id_foreign”中引用列“user_id”和引用列“id”不兼容。
我的迁移如下所示:
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');
});
}
用户表
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');
});
}
有人能告诉我我做错了什么吗?我无法解决这个问题。
谢谢。
问候。
【问题讨论】:
-
外键的类型必须和我们要引用的id完全一致。在这种情况下,您必须将外键列类型更改为 unsignedBigInteger 类型。
-
别忘了运行 php artisan migrate:fresh 来重建整个表格。
-
使用这个
$table->unsignedBigInteger('user_id')->nullable(); -
仍然显示相同的错误:(
-
$table->id(); //UNSIGNED BIG INTEGER所以$table->unsignedBigInteger('empresa_id')