Search code examples
phplaravellaravel-5migrationlaravel-artisan

Laravel php artisan migrate errors on empty database


Weird things are happening. Been trying to run migrations with php artisan migrate but get the following error about a missing table (that is supposed to be created and populated by migrations).

PHP Fatal error:  Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'app.portals' doesn't exist in /home/daniel/Programming/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:80
Stack trace:
#0 /home/daniel/Programming/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(80): PDO->prepare('select * from `...', Array)
#1 /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(326): Doctrine\DBAL\Driver\PDOConnection->prepare('select * from `...')
#2 /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(657): Illuminate\Database\Connection->Illuminate\Database\{closure}('select * from `...', Array)
#3 /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(624): Illuminate\Database\Connection->runQueryCallback('select * from `...', Array, Object(Closure))
#4 /home/daniel/Programming/app/vendor/laravel in /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664
PHP Fatal error:  Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'app.portals' doesn't exist in /home/daniel/Programming/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:80
Stack trace:
#0 /home/daniel/Programming/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(80): PDO->prepare('select * from `...', Array)
#1 /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(326): Doctrine\DBAL\Driver\PDOConnection->prepare('select * from `...')
#2 /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(657): Illuminate\Database\Connection->Illuminate\Database\{closure}('select * from `...', Array)
#3 /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php(624): Illuminate\Database\Connection->runQueryCallback('select * from `...', Array, Object(Closure))
#4 /home/daniel/Programming/app/vendor/laravel in /home/daniel/Programming/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664

Laravel Version: 5.5.*

PHP Version: 7.1

Backstory:

I decided to work on a new local database installation instead of always relying on a remote one. Then I found artisan doesn't work anymore.

Attempts:

Every artisan command I could get my hands on, but none of them worked because even php artisan --help threw the error above...

I also tried cloning the repo as a fresh start, then checking out the branch I'm working on, and running php artisan migrate with the same error.

Migrations:

I can't post all of them, but there is one migration that builds the missing table:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePortalLinks extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('portal_viewer_user', function (Blueprint $table) {
            $table->integer('viewer_user_id');
            $table->integer('portal_id');
        });

        Schema::create('admin_user_portal', function (Blueprint $table) {
            $table->integer('admin_user_id');
            $table->integer('portal_id');
        });

        Schema::create('portals', function (Blueprint $table) {
            $table->increments('id');
            $table->string('identifier');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('viewer_user_portal');
        Schema::dropIfExists('portal_admins');
        Schema::dropIfExists('portals');
    }
}

Solution

  • Cause of Error:

    Error was caused by a Laravel service provider that had a database query in its constructor.

    It turns out - I didn't know about this - that Laravel service providers get instantiated when running artisan.

    Solution:

    Once I put some validation in my service provider to stop the query from happening on artisan commands, running php artisan migrate:install, php artisan migrate:fresh, and php artisan migrate --seed populated by database with all the necessary tables and records.