简体   繁体   中英

Laravel 5 foreign key migration error

i have an issue when i try to reset my migrations. I am using oracle sql so when the problem appeared the first think i've done i emptied the database now in my sqldeveloper i don't see the tables but if i run tinker every table is created. When i am trying to rollback my migrations i recieve an error referenced to the fact that i have foreign keys in some tables: unique/primary keys in table referenced by foreign key, but when i try to remove constraints artisan says that the table does not have this constraint.

Here are my migrations files: categories table:

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

}

articles table:

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

my roles table:

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

my tags table:

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

So, basicaly my database is empty but i can't run anything. If i run php artisan migrate no tables are created, but if i run php artisan migrate:refresh/rollback/reset unique/primary keys in table referenced by foreign key on dropping table articles. I've searched about this but i didn't find anything that suits my problem hope i'll find here the solution

You should dropForeign before you drop a table.

Example:

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

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