Laravel 6 Проблема миграции: ограничение внешнего ключа сформировано неправильно - PullRequest
0 голосов
/ 05 марта 2020

Я знаю, что этот вопрос задавали много раз, и я попробовал все ответы, но я просто не могу отладить эту проблему.

У меня есть миграция с именем 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.

Спасибо за вашу помощь.

Томас

Ответы [ 2 ]

1 голос
/ 05 марта 2020

Ваш products_table имеет отношение с product_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');
    }
}
1 голос
/ 05 марта 2020

Добавление длины строки по умолчанию в вашем AppServiceProvider

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

        Schema::defaultStringLength(191);
    }

}
...