Laravel 5 Migrate Base table or view not found: 1146

前端 未结 12 542
情话喂你
情话喂你 2020-12-11 01:06

I am in big problem. I am trying to rum php artisan migrate to generate table migration, but i am getting

[2016-03-08 05:49:01] local.ERR

相关标签:
12条回答
  • 2020-12-11 02:01

    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.

    0 讨论(0)
  • 2020-12-11 02:02

    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.

    0 讨论(0)
  • 2020-12-11 02:02

    It worked for me, see more here:

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

        try {
    
            return Permission::with('role')->get();
    
        } catch (\Exception $e) {
    
            return [];
    
        }
    
    0 讨论(0)
  • 2020-12-11 02:02

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

    0 讨论(0)
  • 2020-12-11 02:07

    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

    0 讨论(0)
  • 2020-12-11 02:12

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

    0 讨论(0)
提交回复
热议问题