突然我在创建4个表的迁移中出错

时间:2019-03-12 12:22:58

标签: laravel-5 migration

在我的laravel 5.7应用程序中,我使用迁移文件,该文件用了大约5个月的时间,并在1个文件中创建了4个表(我从一个插件使用的最后3个表), 但突然我在运行它时出错:

Migrating: 2018_07_13_150151_create_votes_table

   Illuminate\Database\QueryException  : SQLSTATE[42S02]: Base table or view not found: 1146 Table 'Vt.taggables' doesn't exist (SQL: select `tag_id` from `taggables` where `taggable_id` = 1 and `taggable_type` = App\Vote)

  at /mnt/_work_sdb8/wwwroot/lar/votes/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668| 

  Exception trace:

  1   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'Vt.taggables' doesn't exist")
      /mnt/_work_sdb8/wwwroot/lar/votes/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:63

  2   PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'Vt.taggables' doesn't exist")
      /mnt/_work_sdb8/wwwroot/lar/votes/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:61

  Please use the argument -v to see more details.

这是迁移文件:database/migrations/2018_07_13_150151_create_votes_table.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\User;
use App\Vote;
use App\Taggable as MyTaggable;
use App\Tag;
use App\TagDetail;
use App\VoteCategory;

class CreateVotesTable extends Migration
{
    private $users_tb;
    private $votes_tb;
    private $tags_tb;
    private $taggables_tb;
    private $tag_details_tb;
    private $vote_categories_tb;
    public function __construct()
    {
        $this->users_tb= with(new User)->getTable();
        $this->votes_tb= with(new Vote)->getTable();
        $this->tags_tb= with(new Tag)->getTable();
        $this->taggables_tb= with(new MyTaggable)->getTable();
        $this->tag_details_tb= with(new TagDetail)->getTable();
        $this->vote_categories_tb= with(new VoteCategory)->getTable();
    }
    public function up()
    {
        Schema::create($this->votes_tb, function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 255)->unique();
            $table->string('slug', 260)->unique();
            $table->mediumText('description');

            $table->integer('creator_id')->unsigned();
            $table->foreign('creator_id')->references('id')->on($this->users_tb);//->onDelete('RESTRICT');

            $table->integer('vote_category_id')->unsigned();
            $table->foreign('vote_category_id')->references('id')->on($this->vote_categories_tb);//->onDelete('RESTRICT');


            $table->boolean('is_quiz')->default(false);
            $table->boolean('is_homepage')->default(false);
            $table->enum('status', ['N', 'A', 'I'])->comment(' N=>New,  A=>Active, I=>Inactive');
            $table->integer('ordering')->unsigned();
            $table->string('image', 100)->nullable();

            $table->timestamp('created_at')->useCurrent();
            $table->timestamp('updated_at')->nullable();

            $table->index(['created_at'], 'votes_created_at_index');
            $table->index(['is_quiz', 'status'], 'votes_is_quiz_status_index');
            $table->index(['ordering', 'status'], 'votes_ordering_status_index');
            $table->index(['is_homepage', 'status'], 'votes_is_homepage_status_index');
            $table->index(['creator_id', 'status', 'name'], 'votes_creator_id_status_name_index');
            $table->index(['vote_category_id', 'status', 'name'], 'votes_vote_category_id_status_name_index');


        });

        Schema::create($this->tags_tb, function (Blueprint $table) {
            $table->increments('id');
            $table->json('name');
            $table->json('slug');
            $table->string('type')->nullable();
            $table->integer('order_column')->nullable();
            $table->timestamps();
        });

        Schema::create($this->taggables_tb, function (Blueprint $table) {
            $table->increments('id');
            $table->integer('tag_id')->unsigned();
            $table->integer('taggable_id')->unsigned();
            $table->string('taggable_type');

            $table->foreign('tag_id')->references('id')->on($this->tags_tb)->onDelete('cascade');

        });

        Schema::create($this->tag_details_tb, function (Blueprint $table) {
            $table->increments('id');
            $table->integer('tag_id')->unsigned()->unique();
            $table->foreign('tag_id')->references('id')->on($this->tags_tb)->onDelete('cascade');
            $table->string('image', 100)->nullable();
            $table->text('description');
        });


        Artisan::call('db:seed', array('--class' => 'votesInitData'));


    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->taggables_tb);
        Schema::dropIfExists($this->tag_details_tb);
        Schema::dropIfExists($this->tags_tb);
        Schema::dropIfExists($this->votes_tb);
    }
}

在我看来,我唯一修改过的是使用表名前缀,这是我在任何模型app/Tag.php的构造函数中设置的:

<?php

namespace App;

use Spatie\EloquentSortable\Sortable;
use Illuminate\Database\Eloquent\Model;
use App\Http\Traits\HasSlug;
use App\Http\Traits\funcsTrait;


class Tag extends Model implements Sortable
{
    use funcsTrait;

    public function __construct()
    {
        parent::__construct();
        $this->table = config('app.db_prefix').'tags';
    }

有人知道为什么这个错误以及如何解决吗?

谢谢!

0 个答案:

没有答案