【问题标题】:Laravel php artisan migrate errors on empty databaseLaravel php artisan 迁移空数据库上的错误
【发布时间】:2018-11-01 09:16:35
【问题描述】:

奇怪的事情正在发生。一直在尝试使用 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

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');
    }
}

【问题讨论】:

  • 也许你有一个迁移试图修改一个还不存在的表。没有看到迁移就很难诊断……你能发布那些吗?
  • 你试过'php artisan migrate:refresh'吗?
  • 显示您的迁移。当您开始在本地数据库上工作时,您是否完全迁移了远程数据库?
  • @ka_lin 是的,它给了我同样的错误:(
  • @aynber 所以事实证明,我的一个服务提供商正在其构造函数中查询数据库,从而导致php artisan 在任何迁移运行之前完全失败。运行命令php artisan migrate:install,然后是php artisan migrate:fresh,它用所有表填充了我的数据库!

标签: php laravel laravel-5 migration laravel-artisan


【解决方案1】:

错误原因:

错误是由 Laravel 服务提供者在其构造函数中有数据库查询引起的。

事实证明——我不知道这一点——Laravel 服务提供者在运行 artisan 时会被实例化。

解决方案:

一旦我在我的服务提供商中进行了一些验证,以阻止在工匠命令上发生查询,运行 php artisan migrate:installphp artisan migrate:freshphp artisan migrate --seed,由数据库填充所有必要的表和记录。

【讨论】:

    【解决方案2】:

    您正在尝试刷新。

    你应该试试 php artisan migrate:fresh

    向下命令将不起作用,因为没有要删除的内容。

    当你运行刷新命令时,laravel 将首先尝试使用 down 函数回滚所有内容。然后它将重建所有内容。在服务器上,你从来没有遇到过问题,因为那些表就在那里。但是在新安装的情况下,整个事情都无法回滚。没有什么可以回滚的。

    【讨论】:

    • 没用。不幸的是,在运行与 php artisan 相关的任何事情时,我一直收到此错误:(
    • 错误显示'select * from ...'` 数组似乎表明某处的某些迁移假设某处已存在某个表。有关迁移的更多详细信息?我想到的另一件事是一次运行一个表的迁移。这样您就可以准确确定问题出在哪里。将代码编辑为... public function up(){ Schema::create('portal_viewer_user',function (Blueprint $table) { $table-&gt;integer('viewer_user_id'); $table-&gt;integer('portal_id'); });); }
    • 嗯...似乎没有用。但是,我注意到,当我运行 php artisan 时,我的 PortalService 正在被实例化...我猜这不应该发生?
    • 所以我的 PortalService 在其构造函数中有一个 db 查询,每次我运行 php artisan 命令时都会失败。我添加了一些验证以防止在 CLI 中发生查询,从而修复了我的帖子中的错误!然后我运行php artisan migrate:installphp artisan migrate:fresh,它们在我的数据库中创建了所有表。
    猜你喜欢
    • 1970-01-01
    • 2017-06-21
    • 2014-10-05
    • 2020-06-28
    • 2017-06-11
    • 2014-06-05
    • 2021-08-21
    • 2019-02-03
    • 1970-01-01
    相关资源
    最近更新 更多