SQLSTATE [HY000]:一般错误:1005无法创建表Data。company_eligibilities(错误号:150“外键约束格式不正确”)

时间:2020-08-12 11:51:58

标签: laravel laravel-7 laravel-migrations

 public function up()
    {
        Schema::create('company_eligibilities', function (Blueprint $table) {
            $table->id();
            $table->string('marks');
            $table->integer('company_id')->unsigned();
            $table->timestamps();

            $table->foreign('company_id')
                ->references('id')->on('companies')
                ->onDelete('cascade')
                ->onUpdate('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('company_eligibilities');
    }
    
     

公司迁移已在执行上述迁移之前完成

当我尝试与外键一起创建表时,出现上述错误。我做错什么了吗?

1 个答案:

答案 0 :(得分:1)

请确保在迁移companies之前先迁移company_eligibilities,然后将外键ID更改为 无符号大整数,Laravel 5.8将默认外键设置为无符号大整数,对于 6.x 7.x

$table->bigInteger('company_id')->unsigned();

或者,

$table->unsignedBigInteger('company_id');