23

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
Mandy
  • 1,103
  • 3
  • 17
  • 38
  • Can you please share the migration file code? – Ashwani Goyal Mar 08 '16 at 06:13
  • updated, kindly check. – Mandy Mar 08 '16 at 06:22
  • even i am not able to run php artisan make or any other command. I am getting same error for creating controller or model. – Mandy Mar 08 '16 at 06:23
  • Can you post your .env file, especially which related to database connection? – Zamrony P. Juhara Mar 08 '16 at 06:37
  • Updated with .env file please check. – Mandy Mar 08 '16 at 06:40
  • The possible reason is you already have `permissions` and/or `roles` tables in your target database `(testing as per your .env)`. Try deleting these tables before you run the `migration` command. If that is not the case then some of your other migration that apparently run before this migration is creating those table. Please write here if that is the case or not – Ashwani Goyal Mar 08 '16 at 06:46
  • No, Database is blank. I have drop all the tables from DB, after that i am trying to migrate. – Mandy Mar 08 '16 at 06:47
  • And what about the other migrations in your `database/migrations` directory? Are we sure that other migrations aren't doing anything with these tables. – Ashwani Goyal Mar 08 '16 at 06:54
  • Yes, i have checked. other migration not using it. I have created seeders. – Mandy Mar 08 '16 at 06:57
  • I have resolved my issue. Thanks every one for the support. – Mandy Mar 08 '16 at 07:11
  • 1
    You resolved your issue. Can you please share how you solved it? Struggling with the same exact problem. – skoTner Jan 26 '17 at 11:19
  • Kindly check the name and order of your migration, migration will execute in the order. – Mandy Jan 28 '17 at 15:26
  • Same problem, it seems Laravel executes AppServiceProvider code, even for migrations. I commented everything in the boot function, migrate, and uncomment. Worked for me. – Vincent Decaux Sep 13 '19 at 11:09

13 Answers13

28

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.

Vladimir Salguero
  • 5,609
  • 3
  • 42
  • 47
18

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

Dardan Iljazi
  • 747
  • 1
  • 9
  • 16
12

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.

Jino Antony
  • 365
  • 3
  • 8
4

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.

4

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.

Eugene P.
  • 1,517
  • 12
  • 16
2

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.

Mo D Genesis
  • 5,187
  • 1
  • 21
  • 32
1

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.

Artenes Nogueira
  • 1,422
  • 1
  • 14
  • 23
1

It worked for me, see more here:

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

    try {

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

    } catch (\Exception $e) {

        return [];

    }
Cesar Devesa
  • 990
  • 6
  • 14
0

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

Dharmesh Patel
  • 1,881
  • 1
  • 11
  • 12
  • I am not using default providers except collactive Html. I have also removed all code from route. still getting same error. – Mandy Mar 08 '16 at 06:39
  • Well in my case this worked. I don't get though why it has any impact on the migrations... Does migrate boot laravel and check code? – Goowik Aug 03 '17 at 13:39
0

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

Andy
  • 333
  • 4
  • 10
0

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

ffuentes
  • 1,042
  • 5
  • 16
  • 36
0

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.

Phantom1412
  • 319
  • 4
  • 9
0

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