为了账号安全,请及时绑定邮箱和手机立即绑定

在 laravel 中运行迁移时出现意外错误(表不存在)

在 laravel 中运行迁移时出现意外错误(表不存在)

PHP
qq_笑_17 2023-10-22 21:57:06
我有很多迁移要迁移,但我的数据库现在是空的。当我运行时php artisan migrate出现此错误:PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'shop.permissions' doesn't exist")这是我的权限迁移:Schema::create('permissions', function (Blueprint $table) {        $table->id();        $table->string('name');        $table->string('label')->nullable();        $table->timestamps();    });    Schema::create('permission_user', function (Blueprint $table) {        $table->unsignedBigInteger('permission_id');        $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');        $table->unsignedBigInteger('user_id');        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');        $table->primary(['permission_id', 'user_id']);    });    Schema::create('roles', function (Blueprint $table) {        $table->id();        $table->string('name');        $table->string('label')->nullable();        $table->timestamps();    });    Schema::create('permission_role', function (Blueprint $table) {        $table->unsignedBigInteger('permission_id');        $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');        $table->unsignedBigInteger('role_id');        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');        $table->primary(['permission_id', 'role_id']);    });    Schema::create('role_user', function (Blueprint $table) {        $table->unsignedBigInteger('user_id');        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');        $table->unsignedBigInteger('role_id');        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');        $table->primary(['user_id', 'role_id']);    });当我在数据库中创建权限手册并运行php artisan migrate它时不会出现此错误,但经过一些迁移后我收到此错误:SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'permissions' already exists我可以做什么来迁移我的表?
查看完整描述

1 回答

?
森栏

TA贡献1810条经验 获得超5个赞

通常,当您让服务提供商查询权限(通常是为了设置授权门)时,就会出现这个问题。每次启动应用程序(包括在控制台中)时都会启动服务提供程序。当数据库为空时,没有可查询的表。


如果您出于授权目的执行此查询,则在控制台中运行时可能不需要执行此操作,因为没有传入的 Web 请求。如果您愿意,可以尝试将您正在执行的操作设置为有条件的尝试如果在控制台中运行则不运行;在服务提供商中:


if (! $this->app->runningInConsole()) {

    // not running in console

}


查看完整回答
反对 回复 2023-10-22
  • 1 回答
  • 0 关注
  • 77 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信