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

Viewed 28073

I am trying to rum php artisan migrate to generate table migration, but I am getting an error:

[2016-03-08 05:49:01] local.ERROR: exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testing.permissions' doesn't exist' in D:\xampp\htdocs\LMS-testing\vendor\laravel\framework\src\Illuminate\Database\Connection.php:333

I have tried Base table or view not found: 1146 Table Laravel 5 and Doing a Laravel tutorial, getting "Base table or view not found: 1146 Table 'sdbd_todo.migrations' doesn't exist" but did not succeed.

I have also tried to run php artisan list but getting the same error.

Updated

**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
13 Answers

Check your migration file, maybe you are using Schema::table, like this:

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

If you want to create a new table you must use Schema::create:

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

Laracast More information in this link.

I just had the same problem.

My solution was to comment what I had put into the boot method of AppServiceProvider (because in there I had Model request that didn't exist more).

I ran it to a similar problem.

Seems like I executed a Model query in the routes file. So it executes every time, even before running the actual migration.

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

So I changed it to

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

And everything works fine. Make sure that no Model queries is excecuted on the execution path and then try migrating.

I had this problem when I tried to migrate new database. I had a function in AuthServiceProvider which selects all permissions. I commented that function and the problem was fixed.

The problem is that the foreign keys are added but cannot find the table because it hasn't been created.

1) make tables without foreign keys

2) Make a 9999_99_99_999999_create_foreign_keys.php file

3) put there the foreign keys you want. With the 999..9 .php file it makes sure that it does the foreign keys last after the tables have been made.

4) You have added the tables first and then added the foreign keys. This will work.

Check all of your service providers. In one of the boot methods, a model may be called, the migration to which has not yet been run.

In case anybody else runs in to this I just had the same issue and the reason it was happening was because I had created several commands and then needed to rollback my db to rerun the migrations.

To fix it I had to comment out the contents of the

protected $commands = []

in app\Console\Kernel.php file.

Phew!!!! :-)

The only solution that worked for me was to disable PermissionsServiceProvider in config/app.php before migrating.

For those who end up being here because of the "migrations" table is not created automatically. In some cases you need to run the following artisan command:

php artisan migrate:install

OR with code:

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

to get your base "migrations" table, not sure why it is not generated automatically but it does happened.

Please Check your boot method on AppServiceProvider. if you're using any model request on there, please comment them and migrate again. Your problem will be resolved

Related