Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations Fail on Rollback attempt: #364

Open
Bibben opened this issue Oct 25, 2024 · 1 comment
Open

Migrations Fail on Rollback attempt: #364

Bibben opened this issue Oct 25, 2024 · 1 comment

Comments

@Bibben
Copy link

Bibben commented Oct 25, 2024

Attempting to rollback migrations causes a fail because of the missing index: 'original_media_id'

"plank/laravel-mediable": "^6.1",
MYSQL: Server version: 8.0.39 MySQL Community Server - GPL

All that was done was install the package, publish the config, make a new media model that implements the media and expands on it.
After getting some error after trying to publish the migrations and making changes i got this error.

Trying to fix it by removing my changes to the published migrations.
Presented the same problem with the same failure on attempt to rollback the migration.

steps to reporduce:
-install
-migrate
-rollback

$ php artisan migrate

   INFO  Running migrations.  

  2016_06_27_000000_create_mediable_tables .............................................................................................. 525ms DONE
  2020_10_12_000000_add_variants_to_media ............................................................................................... 257ms DONE
  2024_03_30_000000_add_alt_to_media ..................................................................................................... 40ms DONE

$ php artisan migrate:rollback --step=3

   INFO  Rolling back migrations.  

  2024_03_30_000000_add_alt_to_media .............................................Server version: 8.0.39 MySQL Community Server - GPL
........................................................ 47ms DONE
  2020_10_12_000000_add_variants_to_media ................................................................................................. 2ms FAIL

   Illuminate\Database\QueryException 

  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'original_media_id'; check that column/key exists (Connection: mysql, SQL: alter table `media` drop foreign key `original_media_id`)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:822
    818▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    819▕                 );
    820▕             }
    821▕ 
  ➜ 822▕             throw new QueryException(
    823▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    824▕             );
    825▕         }
    826▕     }

      +35 vendor frames 

  36  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()

Proposed solution to 2020_10_12_000000_add_variants_to_media:

 if (DB::getDriverName() !== 'sqlite') {
      $table->dropForeign('media_original_media_id_foreign'); // The actual foreign key index.
      $table->dropColumn('original_media_id');
  }

Or is this just an issue specific to me due to mysql version or other environment factors on my end?

@Bibben
Copy link
Author

Bibben commented Oct 25, 2024

Or more intuitively, making the dropForeign use an array instead:
if (DB::getDriverName() !== 'sqlite') { $table->dropForeign(['original_media_id']); $table->dropColumn('original_media_id'); }

Because without it; it doesn't create the index name before trying to drop it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant