Home > Net >  Laravel doesn't drop table on rollback or refresh
Laravel doesn't drop table on rollback or refresh

Time:11-08

I created a new migration called "listings", I basically just followed TraversyMedia's crash course to help my learning process of PHP and Laravel.

I wanted to add a simple modification to the listings migration so it references a user ID (the 'poster').

I did that like this:

$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');

I did migrate:refresh, even tried migrate:rollback then migrate again, but still: whatever I do, the listings table will never be dropped, I have to do it manually. Every other table gets dropped but this and I don't understand why.

It had a "down" function too:

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('listings');
}

Did I do something wrong? Did I miss something?

I use: Laravel 9.38.0, PHP 8.1.10 with Zend Engine v4.1.10

CodePudding user response:

Probably you're using MyISAM storage engine.

MyISAM is a non-transactional storage type, and any write option needs to be rolled back manually (if needed). InnoDB is a transaction storage type that automatically rollbacks the writes if they are not completed.

In your /config/database.php file change mysql data as :

'engine' => null,

to

'engine' => 'InnoDB',

This above method will apply on your all database tables.

If you want to use InnoDB on specific table then add this line on your migration file as :

Schema::create('listings', function (Blueprint $table) {
    $table->engine = 'InnoDB';

    // ...
});

See Database Connection & Table Options

CodePudding user response:

Do you have any records in "listings"? Probably foreign key constraint blocks table being dropped. At first drop your foreign key constraint or just do not use foreign key constraints at all.

  • Related