Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php SQLSTATE[HY000]:一般错误:3780引用列';用户id';和参考列';id';在这种情况下,外键是不兼容的_Php_Mysql_Laravel - Fatal编程技术网

Php SQLSTATE[HY000]:一般错误:3780引用列';用户id';和参考列';id';在这种情况下,外键是不兼容的

Php SQLSTATE[HY000]:一般错误:3780引用列';用户id';和参考列';id';在这种情况下,外键是不兼容的,php,mysql,laravel,Php,Mysql,Laravel,我在Laravel中进行迁移,当我继续执行命令PHP artisan migrate时,会发生此错误: 在Connection.php第664行: SQLSTATE[HY000]:一般错误:3780引用列“user\u id”和外键约束“almacen\u movimentos\u user\u id\u foreign”中引用列“id”不兼容。(SQL:alter tablealmacen\u movimentosadd constraintalmacen\u movimentos\u use

我在Laravel中进行迁移,当我继续执行命令
PHP artisan migrate
时,会发生此错误:

在Connection.php第664行:

SQLSTATE[HY000]:一般错误:3780引用列“user\u id”和外键约束“almacen\u movimentos\u user\u id\u foreign”中引用列“id”不兼容。(SQL:alter table
almacen\u movimentos
add constraint
almacen\u movimentos\u user\u id\u foreign
外键(
user\u id
)引用
users
id

)关于删除(限制)

在PDOStatement.php第129行中:

SQLSTATE[HY000]:一般错误:3780引用列“user\u id”和外键约束“almacen\u movimentos\u user\u id\u foreign”中引用列“id”不兼容

我的迁移如下所示:

阿尔马森努莫维曼托斯表

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为未签名的bigint的主键定义为主键,而在
almacen\u movimentos
表中,引用的
用户id
定义为int

改变

$table->integer('user_id');


主键和外键的结构和数据类型必须相同

您还可以更优雅地解决在
almacen_movimentos的模式中更改的问题
您应该更改:

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

查看文档,您可以从Laravel 7在以下位置执行此操作:

基于@davide casiraghi的回答,这在Laravel 8.5上对我有效

$table->foreignId('user_id')
    ->references('id')
    ->on('users')
    ->onDelete('cascade')
    ->onUpdate('cascade');

外键的类型必须与我们要引用的id完全相同。在这种情况下,您必须将外键列类型更改为unsignedBigInteger类型。并且不要忘记运行php artisan migrate:fresh来重建整个表。使用此
$table->unsignedBigInteger('user_id')->nullable()
仍然显示相同的错误:(
$table->id();//无符号大整数
所以
$table->unsignedbiginger('empresa_id')
@Rodri6uez请确保这两个字段都有相同的数据
$table->foreignId('user_id')->constrained()->onDelete('restrict');
$table->foreignId('user_id')
    ->references('id')
    ->on('users')
    ->onDelete('cascade')
    ->onUpdate('cascade');