【问题标题】:SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key are incompatibleSQLSTATE [HY000]:一般错误:3780 外键中引用列'user_id'和引用列'id'不兼容
【发布时间】: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)引用usersid
) 删除限制)

在 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')

标签: php mysql laravel


【解决方案1】:

users 表中,您将主键定义为无符号大整数,在almacen_movimientos 表中,引用的user_id 定义为int

改变

$table->integer('user_id');

$table->unsignedBigInteger('user_id');

PRIMARY KEY 和 FOREIGN KEY 的结构和数据类型必须相同

【讨论】:

  • @Rodri6uez 确保从数据库验证的两个字段都具有相同的 collations
【解决方案2】:

您还可以以更优雅的方式解决 almacen_movimientos 您应该更改的架构:

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

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

查看文档,您可以从 Laravel 7 开始: https://laravel.com/docs/8.x/migrations#foreign-key-constraints

【讨论】:

    【解决方案3】:

    确保外键数据类型与其引用列相同

    【讨论】:

      【解决方案4】:

      按照数据类型解决这些问题。

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

      注意到本地列 ('id') 设置为 bigIncrements('id'),而外部列设置为无符号大整数。

      【讨论】:

      • 在您的迁移文件中执行此操作,并确保您在外国之前创建所有本地人。例如,在产品之前创建用户,因为用户将是产品王国的外国人。
      【解决方案5】:
      $table->increments('id')  
      

      在你的外键中使用 unsignedInteger

      $table->id() 
      
                  
      

      在你的外键中使用 unsignedBigInteger

      【讨论】:

      猜你喜欢
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 2021-08-31
      • 2019-06-23
      • 1970-01-01
      • 2021-06-01
      • 2012-10-24
      相关资源
      最近更新 更多