繁体   English   中英

运行“php artisan migrate”命令时如何修复“未找到基表或视图:1146”错误?

[英]How to fix the "Base table or view not found: 1146" error when running 'php artisan migrate' command?

我正在尝试朗姆酒php artisan migrate以生成表迁移,但出现错误:

[2016-03-08 05:49:01] local.ERROR:异常 'PDOException' 带有消息'SQLSTATE [42S02]:未找到基表或视图:1146 D 中的表'testing.permissions' 不存在': \xampp\htdocs\LMS-testing\vendor\laravel\framework\src\Illuminate\Database\Connection.php:333

我已经尝试找不到基表或视图:1146 表 Laravel 5做 Laravel 教程,得到“未找到基表或视图:1146 表 'sdbd_todo.migrations' 不存在”但没有成功。

我也尝试运行php artisan list但得到同样的错误。

更新

**RolesPermission migration table**

Schema::create('roles', function(Blueprint $table){
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('label');
            $table->string('description')->nullable();
            $table->timestamps();            
        });
        
        Schema::create('permissions', function(Blueprint $table){
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('label');
            $table->string('description')->nullable();
            $table->timestamps();            
        });
        
        Schema::create('permission_role', function(Blueprint $table){
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();
            
            $table->foreign('permission_id')
                    ->references('id')
                    ->on('permissions')
                    ->onDelete('cascade');
            
            $table->foreign('role_id')
                    ->references('id')
                    ->on('roles')
                    ->onDelete('cascade');
            
            $table->primary(['permission_id', 'role_id']);
        });
        
        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')
                    ->onDelete('cascade');
            
            $table->foreign('user_id')
                    ->references('id')
                    ->on('users')
                    ->onDelete('cascade');
            
            $table->primary(['role_id', 'user_id']);
            
        });


.env file
APP_ENV=local
APP_DEBUG=true
APP_KEY=W8YWZe3LCngvZzexH3WLWqCDlYRSufuy

DB_HOST=127.0.0.1
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=log
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

检查您的迁移文件,也许您正在使用 Schema::table,如下所示:

Schema::table('table_name', function ($table)  {
    // ...
});

如果要创建新表,必须使用 Schema::create:

Schema::create('table_name', function ($table)  {
    // ...
});

Laracast 此链接中的更多信息

我只是有同样的问题。

我的解决方案是评论我在AppServiceProviderboot方法中放入的内容(因为在那里我有不存在的模型请求)。

我遇到了类似的问题。

好像我在路由文件中执行了模型查询。 所以它每次都会执行,甚至在运行实际迁移之前。

//Problematic code
Route::view('users', 'users', [ 'users' => User::all() ]);

所以我把它改成了

Route::get('/users', function () {
    return view('users', ['users' => User::all()]);
});

一切正常。 确保在执行路径上没有执行任何模型查询,然后尝试迁移。

当我尝试迁移新数据库时遇到了这个问题。 我在 AuthServiceProvider 中有一个选择所有权限的函数。 我评论了该功能,问题已解决。

问题是添加了外键但找不到表,因为它尚未创建。

1)制作没有外键的表

2)制作 9999_99_99_999999_create_foreign_keys.php 文件

3)把你想要的外键放在那里。 使用 999..9 .php 文件,它可以确保在制作表格后最后执行外键。

4)您首先添加了表,然后添加了外键。 这将起作用。

检查您的所有服务提供商。 在其中一种引导方法中,可能会调用尚未运行迁移到的模型。

我们来看看错误信息:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testing.permissions' doesn't exist

所以表permissions不存在。 现在让我们看看迁移:

Schema::create('permission_role', function(Blueprint $table){
        $table->integer('permission_id')->unsigned();
        $table->integer('role_id')->unsigned();

        $table->foreign('permission_id')
                ->references('id')
                ->on('permissions')
                ->onDelete('cascade');

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

        $table->primary(['permission_id', 'role_id']);
    });

我们可以在这个Schema调用中看到,您正在为permissions表定义一个外键。

有了这个,我们可以假设 Laravel 正在尝试创建这个外键,但是它想要引用的表不存在。

这通常发生在迁移文件的顺序不等于必须创建表的顺序时。 因此,如果我的迁移文件夹中有此顺序的migrations文件:

2016_10_09_134416_create_permission_role_table 
2016_10_09_134416_create_permissions_table 

它们将按该顺序执行。 但是如果我知道permission_role依赖于permissions ,我需要通过更改文件名中的时间戳来更改它们的位置:

2016_10_09_134415_create_permissions_table 
2016_10_09_134416_create_permission_role_table 

在这种情况下,我只更改了create_permissions_table的最后一位数字,因此它将小于create_permissions_role_table的时间戳。 在此之后,不要忘记运行:

composer dump-autoload

所以作曲家可以知道你的变化。

它对我有用,请在此处查看更多信息:

https://stackoverflow.com/a/49300262/5129122

    try {

        return Permission::with('role')->get();

    } catch (\Exception $e) {

        return [];

    }

我曾经遇到过同样的问题,我想问题不在于您的迁移,但看起来在您在数据库中创建权限表之前检查了权限。 确保您的路由中没有添加auth middleware 或注释掉使用config/app.php的权限表的任何服务提供商。 最有可能从路由中删除auth middleware ,直到您生成迁移将解决您的问题

万一其他人遇到这个问题,我也遇到了同样的问题,原因是我创建了几个命令,然后需要回滚我的数据库以重新运行迁移。

为了解决这个问题,我不得不注释掉

protected $commands = []

在 app\Console\Kernel.php 文件中。

呸!!!! :-)

唯一对我有用的解决方案是在迁移之前禁用 config/app.php 中的 PermissionsServiceProvider。

对于那些因为“迁移”表而最终来到这里的人,不会自动创建。 在某些情况下,您需要运行以下工匠命令:

php artisan migrate:install

或使用代码:

Artisan::call('migrate:install', [
    '--database' => 'your_database_connection', // optional
]);

要获取您的基本“迁移”表,不知道为什么它不会自动生成,但它确实发生了。

请在 AppServiceProvider 上检查您的启动方法。 如果您在那里使用任何模型请求,请评论它们并再次迁移。 您的问题将得到解决

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM