Как передать внешний ключ laravel - PullRequest
0 голосов
/ 15 октября 2019

Я пытаюсь передать внешний ключ, но застрял с этой ошибкой Base table or view already exists: 1050 Table 'favorites' already exists" как я могу передать им все внешние ключи и обычный идентификатор, например $table->bigInteger('user_id')->unsigned();

таблицы

 public function up()
{

    Schema::create('favorites', function (Blueprint $table) {
        $table->Increments('id');
        $table->bigInteger('user_id')->unsigned();
        $table->bigInteger('product_id')->unsigned();
        $table->timestamps();

    });

    Schema::create('favorites', function (Blueprint $table){

        $table->bigInteger('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users');

        $table->bigInteger('product_id')->unsigned()->index();
        $table->foreign('product_id')->references('id')->on('products');
    });

}

public function down()
{
    Schema::dropIfExists('favorites');
}

Ответы [ 3 ]

0 голосов
/ 15 октября 2019

Пожалуйста, добавьте это поле таблицы в вашу таблицу

public function up()
{
    Schema::create('favorites', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->bigInteger('product_id')->unsigned();
        $table->foreign('product_id')->references('id')->on('products');
        $table->timestamps();
    });
}
0 голосов
/ 15 октября 2019

Другие ответы верны. Если вы когда-либо пропустили необходимые задачи перед выполнением, попробуйте это

1) Удалите или удалите перенесенную вами таблицу

2) В таблице миграции удалите запись, котораясостоит из имени «избранное»

3) С помощью этой функции скопируйте и вставьте этот код в функцию «вверх»

Schema::create('favorites', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->bigInteger('user_id')->unsigned();
    $table->bigInteger('product_id')->unsigned();
    $table->timestamps();

    $table->foreign('user_id')->references('id')->on('users');
    $table->foreign('product_id')->references('id')->on('products');

});

4) Запустите эту команду еще раз

php artisan migrate
0 голосов
/ 15 октября 2019

Вы переносите таблицу дважды, передавая внешние ключи в той же миграции

public function up()
{
    Schema::create('favorites', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->bigInteger('product_id')->unsigned();
        $table->foreign('product_id')->references('id')->on('products');
        $table->timestamps();
    });
}

public function down()
{
    Schema::dropIfExists('favorites');
}

Надеюсь, это поможет

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...