如何解决在laravel中运行迁移时发生意外错误表不存在
我有很多要迁移的迁移,现在我的数据库为空。
运行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
我该怎么做才能迁移我的表?
解决方法
通常,当您让服务提供商查询权限时经常会出现此问题,通常是设置授权门。每次启动应用程序(包括在控制台中)时,都会启动服务提供者。数据库为空时,没有要查询的表。
如果出于授权目的而执行此查询,则由于没有Web请求进入,因此在控制台中运行时可能不需要这样做。如果您希望尝试所做的一切,如果在控制台中运行,则有条件尝试而不运行;在服务提供商中:
if (! $this->app->runningInConsole()) {
// not running in console
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。