2
votes

While creating and testing migrations files for a MySQL database I receive the following error -

{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.entities' doesn't exist (SQL: select * from `entities`)","file":"\/Applications\/MAMP\/htdocs\/laravel\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php","line":555}}

What does not make sense is that I am using artisan to run the migrations and nothing in my migrations files is trying to run a SELECT query.

The artisan command I am using is php artisan migrate (also tried resetting with php artisan:reset and php artisan:refresh).

The relevant migration file content is:

public function up()
    {
            Schema::create('entities', function(Blueprint $table)
            {
                $table->increments('id')->unsigned();
                //..some more columns..
                $table->timestamps();
            }); 
    }

There are other migration files (total 10 files) but non of them has any dependency on the 'entities' table.

Trying to wrap my head around why artisan is running a SELECT query on the entities table instead of creating the table?

When I manually create the entities table (and add if (!Schema:hasTable('entities') to bypass the creation of the new table), everything works fine so I'm positive that the other migration files are not causing the problem.

Config files and the database connection are all good.

Any help would be greatly appreciated.

Thank you.

3
Sidenote : the increments() function already uses unsigned integers by default. - user2629998
@André thanks, good to know..still banging my head over this problem though...any idea what's going on? - dev7
@André tried, same problem. - dev7
still same problem. I even deleted the database and created a new one. does artisan try to run any of the models/controllers/views in the app folder? cause that's the only reason it I can think of for the SELECT query.. - dev7
@Yani so the migration itself isn't an issue. Do you have code that can interfere with the DB (for example listening to DB events using DB::listen()) ? - user2629998

3 Answers

2
votes

Solution, with the help and pointers by @Andre:

Turns out that Laravel's artisan has dependency on the routes.php file, which was making a call to the database. While some would say it's not best practice to make calls to the database from routes.php, this project requires it (I'm using caching to reduce preformance loss) . The following was causing artisan to crash while doing the migration (inside routes.php):

foreach (Entities::all() as $entity)
{
}

I was able to bypass it by adding the following to the routes.php file:

if (Schema::hasTable('entities'))
{
    foreach (Entities::all() as $entity)
     {
     }
}

Thanks @Andre for the pointers!

0
votes

Check if you have added any code in Kernel.php or any other laravel infra files. artisan migrate invokes routes and kernel PHP files.

Comment the methods which might be querying these tables.

-1
votes

Had the same problem.

If you are working with ACL (Access control list) check the authServiceProvider for you definition of gates an then put the if statement arround it, worked for me.