自定义Artisan命令migrate:multiple执行报错求助
Laravel多目录迁移命令异常问题
环境信息
- Laravel版本:9.52.16
- PHP版本:8.1.4
- 数据库驱动及版本:MySQL
问题描述
自定义Artisan命令php artisan migrate:multiple用于按指定顺序执行多目录迁移,但执行时系统尝试运行已删除的迁移文件2023_04_08_065850_create_business_categories_table,该文件已不存在于任何迁移目录中。
错误信息
public_html$ php artisan migrate:multiple INFO Preparing database. Creating migration table ............................................................................................................... 13ms DONE INFO Running migrations. >>>>>>>> 其他目录的迁移在此处执行正常 INFO Running migrations. 2023_04_07_065850_create_business_categories_table ..................................................................................... 30ms DONE 2023_04_08_065850_create_business_categories_table ...................................................................................... 1ms FAIL Illuminate\Database\QueryException SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'business_categories' already exists (SQL: create table `business_categories` (`id` char(36) not null, `name` varchar(255) not null, `deleted_at` timestamp null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci') at vendor/laravel/framework/src/Illuminate/Database/Connection.php:760 756▕ // If an exception occurs when attempting to run a query, we'll format the error 757▕ // message to include the bindings with SQL, which will make this exception a 758▕ // lot more helpful to the developer instead of just the database's errors. 759▕ catch (Exception $e) { ➜ 760▕ throw new QueryException( 761▕ $query, $this->prepareBindings($bindings), $e 762▕ ); 763▕ } 764▕ } +9 vendor frames 10 Modules/BusinessService/Database/Migrations/2023_04_08_065850_create_business_categories_table.php:21 Illuminate\Support\Facades\Facade::__callStatic() +25 vendor frames 36 artisan:37 Illuminate\Foundation\Console\Kernel::handle()
自定义命令代码
app\Console\Commands\RunMultipleMigrations.php:
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class RunMultipleMigrations extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'migrate:multiple'; /** * The console command description. * * @var string */ protected $description = 'Run migrations from multiple folders in specific sequence '; public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return int */ public function handle() { $this->call('migrate', ['--path' => 'database/migrations']); $this->call('migrate', ['--path' => 'Modules/HRManagement/Database/Migrations']); $this->call('migrate', ['--path' => 'Modules/FleetService/Database/Migrations']); $this->call('migrate', ['--path' => 'Modules/BusinessService/Database/Migrations']); $this->call('migrate', ['--path' => 'Modules/DeliveryService/Database/Migrations']); $this->call('migrate', ['--path' => 'Modules/FinanceService/Database/Migrations']); $this->info('Multiple migrations of project ran successfully'); } }
命令配置
tinker.php中的配置:
'commands' => [ app\Console\Commands\RunMultipleMigrations::class ],
迁移目录情况
确认2023_04_08_065850_create_business_categories_table不存在于任何迁移目录中。
注意事项
- 仅在生产服务器通过SSH执行时出现该问题,本地环境运行正常。
已尝试的解决方法
- 缓存清理:执行
php artisan config:clear和php artisan cache:clear。 - 数据库核查:确认数据库中
business_categories表不存在。 - 单独执行迁移:运行
php artisan migrate --path=Modules/BusinessService/Database/Migrations/2023_04_07_065850_create_business_categories_table.php执行正常,但自定义命令仍尝试运行已删除的迁移文件。
更新说明
2023_04_08_065850_create_business_categories_table确实不存在于项目目录中。生产服务器SSH执行场景下,清理缓存后系统仍追踪已删除的旧迁移,FinanceService目录也出现类似问题,命令尝试运行已删除的2023_08_10_043908_create_wallet_deductions_table。
内容的提问来源于stack exchange,提问作者Faizan Kamal
相关产品推荐
相关产品推荐

