1
votes

Laravel Version: 5.7

PHP Version: 7.2.9

Database Driver & Version: mysql

Description Last migration creates table but is not registered in the migration table

Steps To Reproduce I have 14 migrations . If I run php artisan migrate The table is created but not registered as a run migration I've dropped the homestead database a couple of times

my migration status table

eventually when I want to refresh ,rollback or migrate for developement purposes I get this error telling me the table exists but however is not on the migration

vagrant@kakbima:~/code$ php artisan migrate
Migrating: 2018_12_13_091954_create_product_subcategories_table

Illuminate\Database\QueryException : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'product_subcategories' already exists (SQL: create table product_subcategories (id int unsigned not null auto_increment primary key, name varchar(191) not null, product_category_id int unsigned not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

public function up()
    {
        Schema::create('product_subcategories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->unsignedInteger('product_category_id');
            $table->foreign('product_category_id')
                    ->references('id')->on('product_categories')
                    ->onUpdate('cascade')
                    ->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS = 0');
        Schema::dropIfExists('product_subcategories');
        DB::statement('SET FOREIGN_KEY_CHECKS = 1');
    }
1
would you like to show us your migration code?Roman Meyer
Sounds like you might have a typo or the like in the down() method of that migrationkerrin
No, I know this cause when I add a nother migration the last migration is never registered in the migration table but the rest are . However the table is still createdJames Nganga

1 Answers

0
votes

I ran into the same issue and the solution was From https://laracasts.com/discuss/channels/laravel/migrated-migration-files-not-listed-in-migration-table?page=1 Here is the post:

Okay, so... For anyone landing on this question after some Googling trying to figure out why this was happening (and the collation/charset not being the issue), here's what we found.

Our first migration was just a simple \DB::unprepared(file_get_contents(DIR.'/../initial.sql'));, which imports an existing database (made before we started using migrations). What we found was that 3-4 migrations in, it just stopped registering them as "ran", even though the migrations were being run (and the changes were committed to the database).

Weird! Eventually, we found that at the top of the .sql file mentioned above, there's a sneaky line that says SET AUTOCOMMIT = 0;, and there's nothing setting it back to its initial value at the end of the file. This caused everything in the migrations to mess up, and it not registering some migrations as having ran anymore.

Simply removing SET AUTOCOMMIT = 0; from the .sql file alleviated the issue for us.

I hope this helps someone else, and if not, I hope I find this answer sometimes in the future when I inevitably run into the same issue again in a year 😛