我从一个基于Laravel 5.2构建的自学项目开始,我发现了我的第一个问题:迁移中的自引用 .
这就是文件 2016_08_02_024942_create_navigation_table.php
的样子(我已删除评论,因为这篇文章没有太长时间):
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateNavigationTable extends Migration
{
public function up()
{
Schema::create('navigation', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id')->unsigned();
$table->integer('position')->unsigned();
$table->string('title');
$table->string('slug');
$table->string('permissions')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('articles');
}
}
然后因为我在这里阅读了几个帖子,如this,this,this以及更多我使用以下代码创建了另一个名为 2016_08_02_030158_add_parent_to_navigation_table.php
的关系文件:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddParentToNavigationTable extends Migration
{
public function up()
{
Schema::table('navigation', function (Blueprint $table) {
$table->foreign('parent_id')->references('id')->on('navigation')->onUpdate('cascade')->onDelete('cascade');
});
}
public function down()
{
Schema::drop('articles');
}
}
但是当我运行命令 php artisan migrate
时出现以下错误,我不确定我做错了什么:
[Illuminate \ Database \ QueryException] SQLSTATE [42S01]:基表或视图已存在:1050表'导航'已存在(SQL:创建表导航(id int unsigned not null auto_increment主键,position int unsigned not null,title varc har(255)not null,slug varchar(255)not null,permissions varchar(255)null,created_at timestamp null,updated_at timestamp null,deleted_at timestamp null)默认字符集utf8 collate utf8_unicode_ci engin e = InnoDB)[PDOException] SQLSTATE [42S01]:基表或视图已存在:1050表'导航'已存在
可以给我一些建议吗?我做错了什么?我看到this package但我不确定它是否能解决我的问题 .
1 回答
意味着 you have same table name in database
所以你需要验证,你没有 table 与数据库中的 name .
确保没有具有相同名称的表名
再次运行迁移
php artisan migration:rollback
由于各种原因,有时无法删除表 .