Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class not found error after renaming migration, despite dumpautoload and clearing cache

I have a simple laravel project with migrations that run fine. I want to make one of them run first (so I can use that table in a foreign key constraint in a later migration), so I renamed that migration script by prepending "1_" so it's first in the list.

But when I run migrations since renaming the file, I get a fatal 'class not found' error for Class '152152CreatePeopleTable', from Migrator.php on line 324- see full stack trace below. (And when I rename the migration file back, the class is found again when I run migrations).

I updated the name in vendor/composer/autoload_classmap.php to match the updated file name (1_2015_09_06_152152_create_people_table.php), and searching in phpstorm, '152152_create_people' isn't found anywhere else except laravel.log, so there should be nowhere else that could have the old file name, right?

So I cleared cache and tried other recommendations from similar-sounding issues (thank you google/stackoverflow):

  • php artisan cache:clear
  • composer clearcache
  • composer dumpautoload
  • composer update, just in case
  • I made sure I had a fresh migrations table too; I deleted ALL of my database tables (including migrations), then ran 'php artisan migrate:install' (to create a new migrations table) and 'php artisan migrate' to make sure everything runs totally fresh, still the same error
  • hail mary: tried adding controllers, models, and migrations into autoload classmap in composer.json, as per these posts (didn't help so I took it back out): "Class not found" error even after dump-autoload? and Laravel 4 migrate rollback problems
  • note: I'd initially started with an old sqlite file during setup and then changed to a postgres database. Juuust in case that was being referenced at all, I commented out that section in database.php, and I deleted the old sqlite file from the project

What else should I be checking/trying?

[2015-09-12 22:42:19] local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class '152152CreatePeopleTable' not found' in /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:324
Stack trace:
#0 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(133): Symfony\Component\Debug\Exception\FatalErrorException->__construct()
#1 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(118): Illuminate\Foundation\Bootstrap\HandleExceptions->fatalExceptionFromError()
#2 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(0): Illuminate\Foundation\Bootstrap\HandleExceptions->handleShutdown()
#3 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(129): Illuminate\Database\Migrations\Migrator->resolve()
#4 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(112): Illuminate\Database\Migrations\Migrator->runUp()
#5 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(85): Illuminate\Database\Migrations\Migrator->runMigrationList()
#6 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php(74): Illuminate\Database\Migrations\Migrator->run()
#7 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(502): Illuminate\Database\Console\Migrations\MigrateCommand->fire()
#8 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(502): call_user_func_array:{/home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:502}()
#9 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php(150): Illuminate\Container\Container->call()
#10 /home/vagrant/Code/Family-laravel/vendor/symfony/console/Command/Command.php(259): Illuminate\Console\Command->execute()
#11 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Symfony\Component\Console\Command\Command->run()
#12 /home/vagrant/Code/Family-laravel/vendor/symfony/console/Application.php(878): Illuminate\Console\Command->run()
#13 /home/vagrant/Code/Family-laravel/vendor/symfony/console/Application.php(195): Symfony\Component\Console\Application->doRunCommand()
#14 /home/vagrant/Code/Family-laravel/vendor/symfony/console/Application.php(126): Symfony\Component\Console\Application->doRun()
#15 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(100): Symfony\Component\Console\Application->run()
#16 /home/vagrant/Code/Family-laravel/artisan(36): Illuminate\Foundation\Console\Kernel->handle()
#17 /home/vagrant/Code/Family-laravel/artisan(0): {main}()
#18 {main}  
like image 559
Diane Kaplan Avatar asked Sep 12 '15 23:09

Diane Kaplan


People also ask

Why does the Migration Service fail to inject the move request?

In such situation, the migration service failed to inject the move request because the user failed validation. This means that we don’t have a move request for this user and therefore will have no move report.

How to check if a migration user has not been moved?

In cases where the error message on the migration user is not so obvious and you still don’t have a move request created for it, you can check Get-MigrationUserStatistics with DiagnosticInfo verbose switch: Get-MigrationUserStatistics <user identity> -DiagnosticInfo verbose |FL and see if any more details found.

What happens if a move request is in a failed state?

Note: For a move request to be in a Failed state, we would need to have a permanent failure. Too many transient failures (usually more than 60) will eventually cause a permanent failure. Too many transient failures can also slow down your migration considerably.

How to fix the class not found in PHP?

So there are a few things you can do to fix the "Class '' not found" in PHP. Typically it's just a typo or a uppercase where it should be lower or vice versa, in the namespace.


1 Answers

Actually Laravel prefixes all migration classes with date so he can stay up to date on the current schema state, when you prefix your class with "1_", Laravel will not be able to parse it hence the error.

As a solution, rename your file to the oldest date in your project, something like 2014_01_06_152152_create_people_table.php then run php artisan dump-autoload (maybe you will need to drop all tables). But, keep in mind that this is not the appropriate solution, because:

Migrations are like version control for your database, allowing a team to easily modify and share the application's database schema.

Indeed, the best option to keep tracking your schema state is to create a new migration artisan make:migration add_person_forein_key_to_{your_table} (maybe later, you no longer need this foreign key so you will just remove the file or create another migration to drop it). If you just started your project it's fine to just rename you migration, but if you work in a large project with many coworkers, I recommend you create a new migration.

like image 122
Razor Avatar answered Oct 02 '22 01:10

Razor