Framework: #php artisan >>> Base table or view not found: ...

Created on 18 Jul 2014  路  15Comments  路  Source: laravel/framework

Tonight after rolling back all my migrations, I did a php artisan migrate and got:

{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'my_database.my_table' doesn't exist (SQL: select * from `my_table` where `enabled` = yes order by `name` asc)","file":"\/vhosts\/mysite.local\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php","line":555}}
root@server:/vhosts/mysite.local

The funny thing is, I've been doing php artisan migrate:refresh for days with this setup without issue.

At first I thought it was some class file was calling my table and I needed to refresh the composer autoload:

# php artisan dump-autoload

SAME ERROR. Then I just tried # php artisan. SAME ERROR.

What is going on? I can't even dump my autoload or run ANY artisan commands. What would be automatically running that is calling my database tables while running the default artisan command?

routes.php only points to controller methods, no SQL in that file.
I removed my view composers as well just incase they were running for some weird reason.
bootstrap/start.php contains no DB queries.
global.php contains no DB queries.

Can't figure this out. And I can't get a more -verbose error to show up.

Most helpful comment

for me , its was in the boot method of the app service provider as i set up a view::share() and i have already changed the database.

All 15 comments

select * from `my_table` where `enabled` = yes order by `name` asc

Any idea where this query could be coming from? a package or your own table?

Yeah if the error is blocking Artisan it's happening somewhere in the files. Might be a package though, try without the service providers you added. Also this is more a question for the forums, not a bug.

I woke up this morning and realized where this was coming from.

For anyone interested, in one of my custom artisan commands, I set my options and arguments for the command based off the contents of a DB table with a query. Since the table didn't exist, it bombed out for ALL artisan commands, not just my custom one. So that is interesting that artisan fetches the options and arguments for each valid artisan command even if you are just running # php artisan.

Also, like I said, I've been doing # php artisan migrate:refresh a lot without this issue and it makes sense now, because when I ran the command, the tables were all rollback and then recreated every time. It was only this time when I did a migrate:rollback, destroying my table, and now I couldn't migrate forward since the table didn't exist anymore.

Yeah sorry realized I should have gone to the forums with this too.

Correction on this for anyone interested. It was the constructor of the command that runs every time and thats where my query was. So that is good to know.

Just php artisan will give a listing of available commands, for which the Symfony Console library needs to instantiate all registered commands to learn about their command name, which is set only on an instance.

That makes sense. Thanks.

for me , its was in the boot method of the app service provider as i set up a view::share() and i have already changed the database.

Thanks for the comment @devqx , I had the same issue and You saved me a lot of time.

@Jakobud
This helped. Good thing you shared. Thanks

@devqx thx!

i make some changes in my migration name
this line: "class CreatePermissionsTable extends Migration"
now when i run any command in Terminal i get this error:

"
In Connection.php line 664:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'note.permissions' doesn't exist (SQL: select * from permissions)

In PDOConnection.php line 79:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'note.permissions' doesn't exist

In PDOConnection.php line 77:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'note.permissions' doesn't exist

"

can any one help me ,please

@z-Shahsavan This is likely not related to the change you made to the migration but more that you're using your permissions table somewhere too early in Laravel's cycle where it crashes because it's missing before it even gets to creating it. Somewhere in a routes file or something. Check my way earlier message.

@Anahkiasen thanks i found error it was from route i used model in route like this
Route::view('users', 'admin.users', [
'data' => AppUser::all()
]);

Thank you @Jakobud

Correction on this for anyone interested. It was the constructor of the command that runs every time and thats where my query was. So that is good to know.

Thank you so much.

I added MyModel::firstOrCreate() in the constructor which caused the bug. There's also no clear error, so it took me a long time until I found your post.

Moving the code from the constructor to handle() solved the problems I had.

Edit: This should be noted in the docs.

Was this page helpful?
0 / 5 - 0 ratings