0

I have many migration to migrate and my database is empty now.

When i run php artisan migrate i got this error :

PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'shop.permissions' doesn't 
exist")

And this is my permissions migration :

Schema::create('permissions', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('label')->nullable();
        $table->timestamps();
    });

    Schema::create('permission_user', function (Blueprint $table) {
        $table->unsignedBigInteger('permission_id');
        $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->primary(['permission_id', 'user_id']);
    });

    Schema::create('roles', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('label')->nullable();
        $table->timestamps();
    });

    Schema::create('permission_role', function (Blueprint $table) {
        $table->unsignedBigInteger('permission_id');
        $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
        $table->unsignedBigInteger('role_id');
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        $table->primary(['permission_id', 'role_id']);
    });

    Schema::create('role_user', function (Blueprint $table) {
        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->unsignedBigInteger('role_id');
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        $table->primary(['user_id', 'role_id']);
    });

When i create permissions manual in my database and run php artisan migrate it work without this error but after some migration i got this error :

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'permissions' already exists

What can i do to migrate my tables ??

2
  • 3
    sounds like you have a service provider that is making a query to gather permissions for some reason ... when the database is empty there isn't a table for it to query Commented Aug 23, 2020 at 9:05
  • exactly, i have a query in my AuthServiceProvider and i comment that and run migration, its work, thank, put your comment in answer Commented Aug 23, 2020 at 9:08

1 Answer 1

1

Often times this is an issue that comes up when you have a Service Provider querying for permissions, often to setup gates for authorization. The Service Providers are booted every time the application is booted which includes in the console. When the database is empty there is no table to query.

If you are doing this query for the purpose of authorization, you may not need to be doing this when running in the console since there is no web request coming in. If you would like you can try making what ever you are doing conditional to try and not run if running in the console; in the Service Provider:

if (! $this->app->runningInConsole()) {
    // not running in console
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.