执行 permissions 迁移的时候报错?

生成迁移数据

    php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations"

在执行permissions迁移的时候报错

php artisan migrate

    In Connection.php line 664:
    SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table '#sql-523_28' (SQL: alter table `model_has_permissions` add cons
      traint `model_has_permissions_permission_id_foreign` foreign key (`permission_id`) references `permissions` (`id`) on delete cascade)

    In Connection.php line 458:
    SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table '#sql-523_28'
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

已解决
1022报错,说明数据库有两个相同的外键,原因是按照教程做第二遍的时候,数据库用的和第一遍一个数据库,所以会有两个相同的permission_idrole_id 外键,所以只需要把这两个外键换个名字就好了,贴上部分代码

  Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
        $table->unsignedInteger('permission_ids');
        $table->unsignedInteger('role_ids');

        $table->foreign('permission_ids')
            ->references('id')
            ->on($tableNames['permissions'])
            ->onDelete('cascade');

        $table->foreign('role_ids')
            ->references('id')
            ->on($tableNames['roles'])
            ->onDelete('cascade');

        $table->primary(['permission_ids', 'role_ids']);
5年前 评论
讨论数量: 1

已解决
1022报错,说明数据库有两个相同的外键,原因是按照教程做第二遍的时候,数据库用的和第一遍一个数据库,所以会有两个相同的permission_idrole_id 外键,所以只需要把这两个外键换个名字就好了,贴上部分代码

  Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
        $table->unsignedInteger('permission_ids');
        $table->unsignedInteger('role_ids');

        $table->foreign('permission_ids')
            ->references('id')
            ->on($tableNames['permissions'])
            ->onDelete('cascade');

        $table->foreign('role_ids')
            ->references('id')
            ->on($tableNames['roles'])
            ->onDelete('cascade');

        $table->primary(['permission_ids', 'role_ids']);
5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!