php - Laravel 5 foreign key migration error -


i have issue when try reset migrations. using oracle sql when problem appeared first think i've done emptied database in sqldeveloper don't see tables if run tinker every table created. when trying rollback migrations recieve error referenced fact have foreign keys in tables: unique/primary keys in table referenced foreign key, when try remove constraints artisan says table not have constraint.

here 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 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 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 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 migrations.  *  * @return void  */ public function down() {     schema::drop('article_tag');     schema::drop('tags'); } 

so, basicaly database empty can't run anything. if run php artisan migrate no tables created, if run php artisan migrate:refresh/rollback/reset unique/primary keys in table referenced foreign key on dropping table articles. i've searched didn't find suits problem hope i'll find here solution

you should dropforeign before drop 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 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'); } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -