猿问

Laravel 6 - MariaDB 10.1:Illuminate\Database\

我有自定义迁移:


代码:


// Groups migration

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

    $table->bigIncrements('id');

    $table->string('name');

    $table->boolean('status')->default(false);

    $table->timestamps();

});


// Clients migration

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

    $table->bigIncrements('id');

    $table->string('fullname');

    $table->integer('phone');

    $table->date('birthday')->nullable();

    $table->boolean('can_get_congratulations')->default(false);

    $table->unsignedInteger('group_id')->default(null);

    $table->foreign('group_id')

          ->references('id')

          ->on('groups')

          ->onDelete('cascade');

    $table->boolean('status')->default(true);

    $table->timestamps();

});

当我运行此迁移文件时,会收到错误消息:


Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table taxisms。#sql-1cc0_65c(errno: 150 "Foreign key constraint is wrongly forms") (SQL: alter table clientsadd constraint clients_group_id_foreignforeign key ( group_id) references groups( id) on delete cascade)

我的迁移代码哪里有错误?



Qyouu
浏览 128回答 1
1回答

守着一只汪

该列需要在两侧匹配。由于groups.id是无符号大整数,因此 group_id 也需要。改变$table->unsignedInteger('group_id')到$table->unsignedBigInteger('group_id')
随时随地看视频慕课网APP
我要回答