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

问题描述 投票:0回答:2

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

我有一个名为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');
    }
}

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

感谢您的帮助。

托马斯

laravel database-migration
2个回答
1
投票

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

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

        Schema::defaultStringLength(191);
    }

}

0
投票

您的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');
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.