未找到Laravel 5 Migrate Base表或视图:1146

问题描述 投票:7回答:10

我遇到了大问题。我正在尝试运行qazxsw poi来生成表迁移,但我得到了

[2016-03-08 05:49:01] local.ERROR:异常'PDOException',消息'SQLSTATE [42S02]:未找到基表或视图:1146 D表中'testing.permissions'不存在': \ XAMPP \ htdocs中\ LMS测试\供应商\ laravel \框架的\ src \照亮\数据库\ Connection.php:333

错误。我试过并在互联网上搜索,我误解了,但我没有得到任何解决方案。

我也尝试过php artisan migrateBase table or view not found: 1146 Table Laravel 5,但我没有成功。

我也试过运行Doing a Laravel tutorial, getting "Base table or view not found: 1146 Table 'sdbd_todo.migrations' doesn't exist",但我又得到同样的错误。我不知道为什么。请建议我的解决方案。

更新

php artisan list
php laravel laravel-5.2
10个回答
6
投票

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

**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 :: create:

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

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


0
投票

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


2
投票

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

1)制作没有外键的表

2)制作一个9999_99_99_999999_create_foreign_keys.php文件

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

4)您先添加了表,然后添加了外键。这会奏效。


2
投票

我把它运行到类似的问题。

好像我在routes文件中执行了Model查询。因此它甚至在运行实际迁移之前每次都会执行。

More information in this link

所以我改成了

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

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


1
投票

我们来看看错误信息:

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

所以表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表的外键。

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

当迁移文件的顺序不等于必须创建表的顺序时,通常会发生这种情况。所以,如果我在permissions文件夹中按此顺序拥有迁移文件:

migrations

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

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 

因此作曲家可以意识到你的变化。


1
投票

我尝试迁移新数据库时遇到此问题。我在AuthServiceProvider中有一个选择所有权限的函数。我评论说功能和问题是固定的。


1
投票

它对我有用,在这里看到更多:

create_permissions_table

create_permissions_role_table

1
投票

我刚遇到同样的问题。

我的解决方案是评论我在composer dump-autoload https://stackoverflow.com/a/49300262/5129122方法中添加的内容(因为在那里我有模型请求不存在更多)。


0
投票

我曾经遇到过同样的问题,我想这个问题不在您的迁移中,但在DB中创建权限表之前会检查权限。请确保您的路线中没有添加 try { return Permission::with('role')->get(); } catch (\Exception $e) { return []; } boot。或注释掉使用AppServiceProvider权限表的任何服务提供商。最有可能从路线中移除auth middleware,直到您生成迁移将解决您的问题


0
投票

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

为了解决它,我不得不评论出来的内容

config/app.php

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

唷!!!! :-)

© www.soinside.com 2019 - 2024. All rights reserved.