简体   繁体   中英

Laravel migration cannot find table

I've got a very strange problem. When I try to migrate my migrations I receive the error:

  [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)  

I already restarted my server. This is the ticket migration:

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

在此处输入图片说明

What could be wrong here?

Change:

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

into:

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

and use composer dump-autoload in your console. Hope it helps =)

Try to separate creation from foreing key alter statement like that

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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