Laravel迁移无法找到表格

时间:2016-09-08 07:18:58

标签: php laravel migration database-migration

我有一个非常奇怪的问题。当我尝试迁移迁移时,收到错误:

  [Illuminate\Database\QueryException]                                         
  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cforum.ticket' d  
  oesn't exist (SQL: alter table `ticket` add `id` int unsigned not null auto  
  _increment primary key, add `title` varchar(255) not null, add `description  
  ` varchar(255) not null, add `user_id` int unsigned not null, add `subject_  
  id` int unsigned not null, add `status_id` int unsigned not null)  

我已经重新启动了我的服务器。这是故障单迁移:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTicketTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('ticket', function (Blueprint $table) {

            $table->increments('id');
            $table->string('title');
            $table->string('description');

            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')
                ->references('id')->on('user')
                ->onDelete('cascade');

            $table->integer('subject_id')->unsigned();
            $table->foreign('subject_id')
                ->references('id')->on('subject')
                ->onDelete('cascade');

            $table->integer('status_id')->unsigned();
            $table->foreign('status_id')
                ->references('id')->on('status')
                ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('ticket');
    }
}

enter image description here

这里可能有什么问题?

2 个答案:

答案 0 :(得分:1)

变化:

Schema::table('ticket', function (Blueprint $table){

成:

Schema::create('ticket', function (Blueprint $table){

并在您的控制台中使用composer dump-autoload。希望它有帮助=)

答案 1 :(得分:1)

尝试将创建与foreing key alter语句分开,如

public function up()
    {
        Schema::create('ticket', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('description');
            $table->integer('user_id')->unsigned();
            $table->integer('subject_id')->unsigned();
            $table->integer('status_id')->unsigned();     
        }
        Schema::table('ticket', function (Blueprint $table) {

            $table->foreign('user_id')
                ->references('id')->on('user')
                ->onDelete('cascade');

            $table->foreign('subject_id')
                ->references('id')->on('subject')
                ->onDelete('cascade');

            $table->foreign('status_id')
                ->references('id')->on('status')
                ->onDelete('cascade');
        });
    }