Im trying to roll back my migrations.
My migrations file uses foreign keys like so
$table->foreign('user_one')->references('id')->on('users'); $table->foreign('user_two')->references('id')->on('users');
My down() function is like so
public function down() { Schema::drop('pm_convo'); Schema::drop('pm_convo_replys'); }
When i run my migrate command
php artisan migrate:refresh --seed --env=local
I am getting the following error
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table `pm_convo`)
Im not exactly sure what to do to fix this.
Edit:
I have tried: $table->dropForeign('pm_convo_user_one_foreign');
But im getting errors with that as well
To delete a foreign key constraintIn Object Explorer, expand the table with the constraint and then expand Keys. Right-click the constraint and then click Delete. In the Delete Object dialog box, click OK.
Foreign Keys Laravel also provides support for adding foreign key constraints to your tables: $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users'); In this example, we are stating that the user_id column references the id column on the users table.
I think this is a better way to do it:
public function down() { DB::statement('SET FOREIGN_KEY_CHECKS = 0'); Schema::dropIfExists('tableName'); DB::statement('SET FOREIGN_KEY_CHECKS = 1'); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With