“如何修复”;“外键约束格式不正确”;PHP应用程序中的SQL查询 问题

“如何修复”;“外键约束格式不正确”;PHP应用程序中的SQL查询 问题,php,laravel,Php,Laravel,我正在用laravel制作一个登录应用程序,我想为用户制作这个角色,但我遇到了这个错误 Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `accounting`.`role_user` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tabl

我正在用laravel制作一个登录应用程序,我想为用户制作这个角色,但我遇到了这个错误

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `accounting`.`role_user` 
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `role_user` add constraint `role_user_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
密码 这是导致上述错误的代码:

CreateUsersTable

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
class CreateRolesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
class CreateRolesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

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

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRoleUser extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('role_user', function (Blueprint $table) {
            $table->integer('role_id')->unsigned();
            $table->integer('user_id')->unsigned();
            $table->foreign('role_id')->references('id')->on('roles')->cascade('delete');
            $table->foreign('user_id')->references('id')->on('users')->cascade('delete');
        });
    }

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

有什么办法解决这个问题吗?

帮不了什么忙,但这不是应该的吗

$table->foreign('role_id')->references('id')->on('roles')->cascade('delete');
$table->foreign('user_id')->references('id')->on('users')->cascade('delete');
取而代之

 $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
 $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

确保两种情况下的数据类型都匹配。主键数据类型和外键数据类型必须相同


其次,确保按顺序放置迁移文件。假设您有三个表roles、users和role\u user。您希望在具有角色\用户的角色和具有角色\用户的用户之间建立关系。在迁移目录中,角色和用户表(迁移文件)应该首先出现,然后是角色用户表(迁移文件),因为角色用户表包含对这两个表的引用。序列在创建文件时进行维护。因此,您可以更改时间(以年\月\日\秒为单位)格式重命名文件,使其位于角色\用户表之前。或者您可以为它们创建一个目录。然后分别迁移它们

角色id和相同数据类型的id可能重复?旁注:您可能正在寻找
onDelete('cascade')
而不是
cascade('delete')
您能进一步解释一下吗?为什么要更换?如果第一个是无效代码,应该有一个明确的通知