【发布时间】:2017-07-19 14:33:17
【问题描述】:
首先我错误地回滚了 2 个迁移,然后我运行了 php artisan migrate 命令并收到以下错误:
[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'exercise1.categories' doesn't exist (SQL: select * from
categories where parent_id = 0)
[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'exercise1.categories' doesn't exist
然后我停止了 Laravel。之后,当我运行 php artisan serve 命令来启动 Laravel 时,我得到了同样的错误。
以下是我回滚的 2 个迁移:
1.
class CreateCategoriesTable extends Migration
{
public function up()
{
Schema::create('categories',function (Blueprint $table){
$table->increments('id');
$table->string('name');
$table->text('parent_id');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('categories');
}
}
2.
class CreateArticlesTable extends Migration
{
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->nullable(false);
$table->longText('article')->nullable(false);
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('articles');
}
}
请帮我解决这个令人沮丧的问题。非常感谢所有答案,在此先感谢。
【问题讨论】:
-
执行
composer dumpautoload然后再次尝试迁移。 -
如果您尝试删除没有
dropIfExists()的表,或者当您为外键引用categories表时,您通常会收到此错误 - 您不是在你的展示代码中做。您是否可能尝试在其他迁移中引用categories表?也许您尝试在以后的迁移中将外键添加到您的parent_id(一个疯狂的猜测)? -
为了进一步调试,请检查 app/storage/logs/laravel.log 中文件的结尾——它准确地指出了问题发生的位置
-
@aynber,我已经尝试过了,没有成功。所有 php artisan 命令都给出相同的错误。
-
@devk 感谢您的建议。我没有在其他迁移中引用
categories表。在后面的迁移中,我没有向parent_id添加外键。
标签: php laravel laravel-migrations