Laravel ошибка добавить photo_id к моей миграции - PullRequest
0 голосов
/ 26 марта 2020

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

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'photo_id' doesn't exist in table (SQL: alter table `scooter` add constraint `scooter_photo_id_foreign` foreign key (`photo_id`) references `photo` (`id`))

я не понимаю, что делаю неправильно.

миграция скутера Класс CreateScooterTable extends Migration {

public function up()
{
    Schema::create('scooter', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->foreign('photo_id')->references('id')->on('photo');
        $table->string("price");
        $table->bigInteger("description");
        $table->string("km");
        $table->string("lincenseplatecolor");
        $table->string("color");
        $table->string("name");
        $table->boolean("sold");
        $table->string("motortype");
        $table->string("brand");
        $table->string("topspeed");
        $table->string("contstructionYear");
        $table->timestamps();
    });
}

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

} перенос фотографий:

class CreatePhotoTable extends Migration

{

public function up()
{
    Schema::create('photo', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('src');
        $table->timestamps();
    });
}


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

}

я не понимаю, что я делаю неправильно, и люблю, если вы, ребята, знаете, awnser (:

1 Ответ

0 голосов
/ 26 марта 2020

Вам необходимо добавить photo_id к вашей миграции.

Это добавит ограничение внешнего ключа, но не создаст столбец photo_id

$table->foreign('photo_id')->references('id')->on('photo');

. Вам потребуется укажите столбец, а затем добавьте ограничение следующим образом:

$table->bigInteger("photo_id")->unsigned()->nullable();
$table->foreign('photo_id')->references('id')->on('photo');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...