我尝试重置迁移时遇到问题。我正在使用oracle sql,所以当问题出现时,首先想到我已经完成了我现在在我的sqldeveloper中清空数据库我没有看到表,但如果我运行修补程序,则每个表都被创建。当我试图回滚我的迁移时,我收到一个错误,该错误引用了我在某些表中有外键的事实:外键引用的表中的唯一/主键,但是当我尝试删除约束时,artisan表示该表没有有这个约束。
以下是我的迁移文件: 类别表:
public function up()
{
Schema::create('categories', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->integer('parent');
});
Schema::create('article_category', function(Blueprint $table){
$table->integer('article_id')->unsigned()->index();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('article_category');
Schema::drop('categories');
}
文章表:
public function up()
{
Schema::create('articles', function(Blueprint $table){
$table->increments('id');
$table->longText('title');
$table->integer('user_id')->unsigned();
$table->string('excerpt',255);
$table->longText('body');
$table->integer('likes')->default(0);
$table->longText('sourceName');
$table->longText('linkURL');
$table->integer('views')->default(0);
$table->timestamp('published_at');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('articles');
}
我的角色表:
public function up()
{
Schema::create('roles', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('role_user', function(Blueprint $table){
$table->integer('role_id')->unsigned()->index();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('role_user');
Schema::drop('roles');
}
我的代码表:
public function up()
{
Schema::create('tags', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('article_tag', function(Blueprint $table){
$table->integer('article_id')->unsigned()->index();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->integer('tag_id')->unsigned()->index();
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('article_tag');
Schema::drop('tags');
}
所以,基本上我的数据库是空的,但我无法运行任何东西。如果我运行php artisan migrate没有创建表,但如果我运行php artisan migrate:刷新/回滚/重置表中由外键引用的表中的唯一/主键。 我已经搜索了这个,但我没有找到任何适合我的问题的希望,希望我能在这里找到解决方案
答案 0 :(得分:0)
在放桌子之前你应该dropForeign
。
示例:
public function up()
{
Schema::create('tags', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('article_tag', function(Blueprint $table){
$table->integer('article_id')->unsigned()->index();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
$table->integer('tag_id')->unsigned()->index();
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('article_tag', function (Blueprint $table) {
$table->dropForeign('article_tag_article_id_foreign');
$table->dropForeign('article_tag_tag_id_foreign');
});
Schema::drop('article_tag');
Schema::drop('tags');
}