2

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.

11
  • Sidenote : the increments() function already uses unsigned integers by default. Commented May 7, 2014 at 0:44
  • @André thanks, good to know..still banging my head over this problem though...any idea what's going on? Commented May 7, 2014 at 0:45
  • @André tried, same problem. Commented May 7, 2014 at 0:46
  • 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.. Commented May 7, 2014 at 0:50
  • 1
    @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()) ? Commented May 7, 2014 at 1:09

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I was calling the database from a provider, which artisan also depends on.
0

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 Comment

This should have been a comment instead of a solution. Unless you're specifically telling the user how to solve his problem, you should post as a comment on his question.
-1

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.

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.