11

First I rolled back 2 migrations by mistake, then I ran php artisan migrate command and I got the following error:

[Illuminate\Database\QueryException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'exercise1.categories' doesn't exist (SQL: select * from categories where parent_id = 0) [PDOException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'exercise1.categories' doesn't exist

Then I stopped Laravel. After that when I run the php artisan serve command for starting Laravel I get the same error. Here are 2 migrations which I've rolled back:

1.

class CreateCategoriesTable extends Migration
    {

        public function up()
        {
            Schema::create('categories',function (Blueprint $table){
                $table->increments('id');
                $table->string('name');
                $table->text('parent_id');
                $table->timestamps();
        });
        }
        public function down()
        {
            Schema::dropIfExists('categories');
        }
    }

2.

class CreateArticlesTable extends Migration
    {
        public function up()
        {
            Schema::create('articles', function (Blueprint $table) {
                $table->increments('id');
                $table->string('title')->nullable(false);
                $table->longText('article')->nullable(false);
                $table->integer('user_id')->unsigned();
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
                $table->timestamps();
            });
        }
        public function down()
        {
            Schema::dropIfExists('articles');
        }
    }

Please help me to solve this frustrating problem. All answers are highly appreciated, thanks in advance.

Mahammad Isgandarli
  • 528
  • 1
  • 7
  • 17
  • Do `composer dumpautoload` then try to migrate again. – aynber Feb 28 '17 at 14:52
  • You usually get this error if you're trying to drop a table without `dropIfExists()` which you have or when you're referencing the `categories` table for a foreign key - which you're not doing in your showcase code. Are you maybe trying to reference `categories` table in some other migration? Maybe you tried to add foreign key to your `parent_id` (a wild guess) in some latter migration? – DevK Feb 28 '17 at 14:55
  • For debugging further check the end of the file in app/storage/logs/laravel.log - which points exactly to where the problem happens – DevK Feb 28 '17 at 14:57
  • @aynber, I've already tried that, no success. All php artisan commands give the same error. – Mahammad Isgandarli Mar 01 '17 at 07:07
  • @devk Thanks for suggestion. I don't reference categories table in other migrations. I didn't add foreign key to parent_id in latter migrations. – Mahammad Isgandarli Mar 01 '17 at 08:12
  • @devk your suggestion about checking log file helped me to find a solution for this problem. Thank you very much. – Mahammad Isgandarli Mar 01 '17 at 08:28
  • For those who end up being here because of the "migrations" table is not created automatically, this [link](https://stackoverflow.com/a/60095493/10539212) might help. – Phantom1412 Feb 06 '20 at 12:59

5 Answers5

32

If you encounter with this problem and if it's not caused by migration files then most probably it happens because of 2 possible reasons.

  1. Check ServiceProviders' boot function if it contains queries that are querying tables that don't exist.
  2. Check if you've created custom helper function and autoloaded that helper function in composer.json file. If custom helper function contains queries that are querying tables that don't exist it will cause this error.

Since ServiceProviders' boot functions and autoloaded custom helper functions are loaded first when laravel is started all the php artisan commands will generate "Base table or view not found" error.

At this point what you should do is comment out those queries that are querying nonexistent tables and run php artisan serve then run php artisan migrate. Then uncomment those lines, save it and everything should work fine.

As @devk suggested it's better to check laravel log files which points exactly to where the problem happens. It led me to find a solution. For this don't forget to Turn on debug mode.

Community
  • 1
  • 1
Mahammad Isgandarli
  • 528
  • 1
  • 7
  • 17
3

When you've rolled back these migrations, you've deleted categories and articles tables. So just run migrate command again:

php artisan migrate
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
3

For me, this error was occurring because of a scheduled task in laravel that involved a database lookup. By commenting out that lookup in laravel/app/Console/Kernel.php from my scheduled task, I was able to migrate my database again.

spencerstewart
  • 131
  • 1
  • 9
0

This may happen due to any of the following problems:

  • Your database setting are not correct. Please check your database settings again.
  • The database name is not correct. May be the database your code is trying to get tables from is not same as the database you mentioned in your database configuration.
Mohit Sharma
  • 331
  • 2
  • 11
0

I also had the same problem. I fixed it, by deleting the database first, then I imported the previous database manually.

And fortunately at that time I still had the previous database backup.

Maybe if you don't have a previous database backup, You can copy all database migrations to a new laravel project, then run 'php artisan migrate' to get the same database as the previous database,