简体   繁体   English

laravel - php artisan 迁移失败

[英]laravel - php artisan migrate fails

I have accidently deleted a database migration table and I have to rollback my changes.我不小心删除了一个数据库迁移表,我必须回滚我的更改。 This ends up failing.这最终失败了。 When I load the items view I get the following error.当我加载项目视图时,出现以下错误。

QueryException in Connection.php line 669: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ushop.items' doesn't exist (SQL: select * from items ) Connection.php 第 669 行中的 QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ushop.items' does not exist (SQL: select * from items )

in Connection.php line 669
at Connection->runQueryCallback('select * from `items`', array(), object(Closure)) in Connection.php line 629
at Connection->run('select * from `items`', array(), object(Closure)) in Connection.php line 342
at Connection->select('select * from `items`', array(), true) in Builder.php line 1461
at Builder->runSelect() in Builder.php line 1447
at Builder->get(array('*')) in Builder.php line 569
at Builder->getModels(array('*')) in Builder.php line 303
at Builder->get(array('*')) in Model.php line 646
at Model::all() in ItemController.php line 15
at ItemController->index()
at call_user_func_array(array(object(ItemController), 'index'), array()) in Controller.php line 80
at Controller->callAction('index', array()) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(ItemController), object(Route), 'index') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
at ControllerDispatcher->callWithinStack(object(ItemController), object(Route), object(Request), 'index') in ControllerDispatcher.php line 54
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ItemController', 'index') in Route.php line 174
at Route->runController(object(Request)) in Route.php line 140
at Route->run(object(Request)) in Router.php line 724
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 726
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699
at Router->dispatchToRoute(object(Request)) in Router.php line 675
at Router->dispatch(object(Request)) in Kernel.php line 246
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 132
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
at Kernel->handle(object(Request)) in index.php line 54
at require_once('/home/thomas/ushop/public/index.php') in server.php line 21

r I have done some research and I have found this and I get the following upon running composer dump-autoload r 我做了一些研究,我发现了这一点,我在运行 composer dump-autoload 后得到以下信息

PHP Fatal error: Class 'Item' not found in /home/thomas/ushop/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php on line 336 PHP 致命错误:在第 336 行的 /home/thomas/ushop/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php 中找不到“项目”类

[Symfony\\Component\\Debug\\Exception\\FatalErrorException] [Symfony\\Component\\Debug\\Exception\\FatalErrorException]
Class 'Item' not found未找到“项目”类

I hope this question hasn't been answered before, but I'm not having much luck so far.我希望这个问题以前没有人回答过,但到目前为止我运气不佳。

It's because you've manually deleted a table in the database so right now the table is not available there but when you try to rolling back the framework trys to undo the migration and hence it looks for the table in the database you've deleted manually so the error occurs.这是因为您已经手动删除了数据库中的表,所以现在该表在那里不可用,但是当您尝试回滚时,框架会尝试撤消迁移,因此它会在您手动删除的数据库中查找表所以错误发生。 Why it looks for the table because in the down method of your migration file you've something like this:为什么要查找该表,因为在迁移文件的 down 方法中,您有如下内容:

Schema::drop('table_name');

So, it trys to delete the already deleted table from the database but it couldn't find and in your migrations table there is a record for every table you've migrated so you can either manually delete all tables and migrate from the beginning or disable the down (By commenting the drop statement) method temporarily so down method will do nothing in that migration file when you roll back.因此,它尝试从数据库中删除已删除的表,但找不到,并且在您的迁移表中,您迁移的每个表都有一条记录,因此您可以手动删除所有表并从头开始迁移或禁用down (通过注释 drop 语句)临时方法,因此当您回滚时,down 方法不会在该迁移文件中执行任何操作。 If you disable the down method temporaarily re-enable the down method again.如果您暂时禁用down方法,请再次重新启用down方法。 It's a hack-ish way to fix the problem tho.这是一种解决问题的黑客方式。

Alternatively, you can add a check in your down method in every migration file or just the deleted table's migration file by checking if the table exists then drop it, for example:或者,你可以在你添加一个检查down方法在每一个迁移文件或只是删除表的迁移文件通过检查table存在,那么drop它,例如:

public function down()
{
    if (Schema::hasTable('table_name')) {
        Schema::drop('table_name');
    }
}

Hope you've got the idea.希望你有这个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM