在控制台上出现以下错误“150 "Foreign key constraint is incorrectly formed"”

getting following error " 150 "Foreign key constraint is incorrectly formed"" on the console

我已经针对其他项目进行了多次检查,但我就是看不出有什么问题。

我附上我的代码:

        Schema::table('datos', function (Blueprint $table) {

            $table->bigInteger('sensor_id')->change();
            $table->foreign('sensor_id')->references('id')->on('sensores')->nullable();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('datos', function (Blueprint $table) {
        $table->dropForeign(['sensor_id']);
    });
}

确保 sensor_id 是 unsignedBigInteger 并在更改之前移动可空方法

您的函数将如下所示:

public function up()
    {
        Schema::create('datos', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->bigInteger('sensor_id')->unsigned();
            $table->foreign('sensor_id')->references('id')->on('sensores')->onDelete('cascade');

            $table->timestamps();
        });
    }

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