Search code examples
phplaravelsqlite

Laravel Migration Issue: Database does not exist


I've been scratching my head for maybe the last hour trying to figure out what's going on.

I deleted my database.sql file since I wanted a new one from scratch. I've done this dozens of times, but upon running php artisan migrate:fresh it returned the following error:

Database file at path [{..}/database/database.sqlite] does not exist. Ensure this is an absolute path to the database. (Connection: sqlite, SQL: PRAGMA foreign_keys = ON;)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:829
    825▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    826▕                 );
    827▕             }
    828▕ 
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }

  1   [internal]:0
      Illuminate\Foundation\Application::Illuminate\Foundation\{closure}(Object(App\Providers\AppServiceProvider))
      +17 vendor frames 

  19  app/Providers/AppServiceProvider.php:24
      Illuminate\Support\Facades\Facade::__callStatic("table")

This has always created the database itself, why is it not now?

I created the database.sqlite in the original directory manually, and it came out with this error instead:

SQLSTATE[HY000]: General error: 1 no such table: kingdoms (Connection: sqlite, SQL: select count(*) as aggregate from "kingdoms")

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:829
    825▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    826▕                 );
    827▕             }
    828▕ 
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }

I thought this might be an issue with the create_kingdoms_table.php migration, so I removed it to see, and somehow, the error is still the same even with the migration gone, even after clearing cache.


Solution

  • The issue that occurred here is that we were running a script when laravel booted that would fetch the kingdoms table.

    But when running fresh migrations, that table would not exist when laravel ran, therefore crashing with the above error.

    We fixed this by creating a handler when laravel was running to only try to fetch the table when a view was executing. In effect turning it into middleware.

    Hope this helps anyone in the future with this problem