Mandy
Mandy

Reputation: 1103

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

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

Upvotes: 23

Views: 38627

Answers (13)

Mehraab Hossain
Mehraab Hossain

Reputation: 68

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

Upvotes: 0

Eugene P.
Eugene P.

Reputation: 1814

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.

Upvotes: 6

Phantom1412
Phantom1412

Reputation: 357

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.

Upvotes: 1

Jino Antony
Jino Antony

Reputation: 364

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.

Upvotes: 12

Dardan Iljazi
Dardan Iljazi

Reputation: 827

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).

Upvotes: 20

ffuentes
ffuentes

Reputation: 1182

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

Upvotes: 0

Vladimir Salguero
Vladimir Salguero

Reputation: 5947

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.

Upvotes: 29

Cesar Devesa
Cesar Devesa

Reputation: 1277

It worked for me, see more here:

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

    try {

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

    } catch (\Exception $e) {

        return [];

    }

Upvotes: 1

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.

Upvotes: 4

Andy
Andy

Reputation: 343

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!!!! :-)

Upvotes: 0

Mo D Genesis
Mo D Genesis

Reputation: 6139

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.

Upvotes: 2

Artenes Nogueira
Artenes Nogueira

Reputation: 1552

Let's look at the error message:

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

So the table permissions doesn't exist. Now let's look at the migrations:

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']);
    });

We can see in this Schema call that you are defining a foreign key to the permissions table.

With this, we can assume that Laravel is trying to create this foreign key, but the table that it wants to refer to doesn't exist.

This usually happens when the order of the migration files is not equal the order in wich the tables must be created. So, if I have the migrations files in this order in my migrations folder:

2016_10_09_134416_create_permission_role_table 
2016_10_09_134416_create_permissions_table 

They will be executed in that order. But if I know that permission_role dependes on permissions, I need to change their positions by changing the timestamp in their file names:

2016_10_09_134415_create_permissions_table 
2016_10_09_134416_create_permission_role_table 

In this case I've changed only the last digit from create_permissions_table so it will be less than the timestamp from create_permissions_role_table. After this, don't forget to run:

composer dump-autoload

So composer can be aware of your change.

Upvotes: 1

Dharmesh Patel
Dharmesh Patel

Reputation: 1891

I faced same problem once, I guess the problem is not in your migrations, but looks like permissions are checked before you create permission tables in DB. make sure you does not have auth middleware added in your route. or comment out any service provider that uses permissions tables from config/app.php. Most probably removing auth middleware from the route till you generate migration will solve your problem

Upvotes: 0

Related Questions