【问题标题】:laravel foreign key in migration return error迁移返回错误中的laravel外键
【发布时间】:2020-09-14 09:15:26
【问题描述】:

迁移运行时出现严重错误

(errno: 150 "外键约束格式不正确")

主键和外键的所有字段都具有相同的数据主键

它们都是无符号大整数

Schema::create('users', function (Blueprint $table) {
              $table->bigIncrements('id');
            $table->string('name');
            $table->string('email',100)->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->integer('rank');
            $table->rememberToken();
            $table->timestamps();
        });



 Schema::create('products', function (Blueprint $table) {
          $table->bigIncrements('id');
          $table->unsignedBigInteger('user_id');

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

    });

【问题讨论】:

    标签: mysql laravel migration laravel-7


    【解决方案1】:

    你可以这样做

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

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 2014-09-27
      • 2018-09-22
      • 2016-07-06
      • 2015-08-14
      • 2016-02-10
      • 2020-01-16
      • 2015-03-02
      • 2019-02-09
      相关资源
      最近更新 更多