dst*_*ssy 1 php migration laravel laravel-5 laravel-artisan
奇怪的事情正在发生。一直在尝试运行迁移,php artisan migrate但出现以下关于缺少表(应该由迁移创建和填充)的错误。
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
Run Code Online (Sandbox Code Playgroud)
Laravel 版本:5.5.*
PHP 版本:7.1
背景故事:
我决定在新的本地数据库安装上工作,而不是总是依赖远程数据库。然后我发现工匠不再工作了。
尝试:
我可以得到的每一个工匠命令,但它们都不起作用,因为甚至php artisan --help抛出了上面的错误......
我还尝试克隆 repo 作为一个新的开始,然后检查我正在处理的分支,并php artisan migrate以相同的错误运行。
迁移:
我不能发布所有这些,但是有一个迁移可以构建丢失的表:
<?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');
}
}
Run Code Online (Sandbox Code Playgroud)
错误原因:
错误是由 Laravel 服务提供者在其构造函数中具有数据库查询引起的。
事实证明 - 我不知道这一点 - Laravel 服务提供者在运行 artisan 时会被实例化。
解决方案:
一旦我在我的服务提供者中进行了一些验证,以阻止在 artisan 命令上发生查询,运行php artisan migrate:install, php artisan migrate:fresh,php artisan migrate --seed并由数据库填充所有必要的表和记录。