1
первая в списке мииграция 
class CreateLoadsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('loads', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('weight');
            $table->timestamps();
        });
    }

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

//вторая миграция
class CreatePointsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('points', function (Blueprint $table) {
            $table->id();
            $table->integer('load_id');
            $table->string('name');
            $table->string('date');
            $table->foreign('load_id')->references('id')->on('loads')->onDelete('cascade');
            $table->timestamps();
        });
    }

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

//ошибка
 Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 3780 Referencing column 'load_id' and referenced column 'id' in foreign key constraint 'points_load_id_foreign' are incompatible. (SQL: alter table `points` add constraint `points_load_id_foreign` foreign key (`load_id`) references `loads` (`id`) on delete cascade)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a
    690▕         // lot more helpful to the developer instead of just the database's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕     }

      +9 vendor frames 
  10  database/migrations/2021_09_28_105239_create_points_table.php:23
      Illuminate\Support\Facades\Facade::__callStatic()

      +21 vendor frames 
  32  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

1 ответ 1

2
не знаю как оно влияет, но заменил
$table->integer('load_id');

на

$table->unsignedBigInteger('load_id');

и все заработало)
2
  • $table->id() делает по дефолту (насколько я помню) unsignedBigInteger, поэтому и поле, которое ссылкается на него (foreign key), должно иметь такой же тип и длину. 30 сен 2021 в 7:58
  • 1
    Чтобы избежать таких ошибок можно использовать вместо двух записей$table->integer('load_id'); и $table->foreign('load_id')->references('id')->on('loads')->onDelete('cascade'); одну: $table->foreignId('load_id')->constrained()->onDelete('cascade'); 30 сен 2021 в 9:34

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.