Laravel 6 迁移问题:外键约束的格式不正确

Laravel 6 Migration problem: Foreign Key Constraint is incorrectly formed

我知道这个问题已经被问了很多,我已经尝试了所有的答案,但我似乎无法调试我遇到的这个问题。

我有一个名为 create_product_image_table

的迁移
class CreateProductImageTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('product_image', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('product_id');
            $table->string('image_url');
            $table->timestamps();

            $table->foreign('product_id')
                ->references('id')
                ->on('products')
                ->onDelete('cascade');
        });
    }

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

另一个名为 create_products_table

的迁移
class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('product_name');
            $table->text('product_description');
            $table->decimal('product_cost', 8, 2);
            $table->decimal('product_price', 8, 2);
            $table->bigInteger('unit_sold')->nullable();
            $table->bigInteger('UPC')->nullable();
            $table->unsignedBigInteger('product_image_id')->nullable();
            $table->timestamps();

            $table->foreign('product_image_id')
                ->references('id')
                ->on('product_image')
                ->onDelete('cascade');
        });
    }

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

它们都具有相同的无符号大整数类型。更改创建迁移的日期并删除 table 以及后来的数据库本身后,我似乎无法通过 Errno 150 错误。

感谢您的帮助。

托马斯

在您的 AppServiceProvider 中添加默认字符串长度

use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {  

        Schema::defaultStringLength(191);
    }

}

您的 products_tableproduct_image_table,

有关系

product_image_table属于products_table

所以你只需要在 product_image_table.

上定义关系

您需要先创建 products_table,它看起来像:

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('product_name');
            $table->text('product_description');
            $table->decimal('product_cost', 8, 2);
            $table->decimal('product_price', 8, 2);
            $table->bigInteger('unit_sold')->nullable();
            $table->bigInteger('UPC')->nullable();
            $table->unsignedBigInteger('product_image_id')->nullable();
            $table->timestamps();
        });
    }

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

然后您需要创建 product_image_table,它看起来像:

class CreateProductImageTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('product_image', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('product_id');
            $table->string('image_url');
            $table->timestamps();
            $table->foreign('product_id')
                ->references('id')
                ->on('products')
                ->onDelete('cascade');
        });
    }

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