Laravel 5 Migrate Base table or view not found: 1146

前端 未结 12 609
情话喂你
情话喂你 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: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.

提交回复
热议问题