Laravel迁移:如何修改ENUM类型字段的默认值?
Got it, let's fix that default value for your assets.status ENUM field. Modifying ENUMs in Laravel can be a bit finicky depending on your database, so here's a reliable approach:
Step 1: Create a New Migration
First, generate a dedicated migration for this change (never edit existing migrations that have been run in production—you'll cause inconsistencies across environments!):
php artisan make:migration update_default_status_on_assets_table
Step 2: Write the Migration Logic
Open the newly created migration file. The approach varies slightly based on your database, but here's what works for most cases:
For MySQL (Most Common)
Laravel's Schema builder doesn't always handle ENUM default value changes smoothly with MySQL, so using a raw SQL statement is the most reliable way:
public function up() { // Update the default value to 'processing' DB::statement("ALTER TABLE assets ALTER COLUMN status SET DEFAULT 'processing'"); // Optional: If you want to update all existing records still using the old default ('active') // DB::table('assets')->where('status', 'active')->update(['status' => 'processing']); } public function down() { // Revert the default value back to 'active' if you ever need to rollback DB::statement("ALTER TABLE assets ALTER COLUMN status SET DEFAULT 'active'"); // Optional: Revert existing records if you updated them in the up() method // DB::table('assets')->where('status', 'processing')->update(['status' => 'active']); }
If 'processing' Isn't in Your ENUM Yet
Wait—if your status ENUM doesn't already include 'processing', you'll need to modify the ENUM definition first before setting the new default:
public function up() { // Update the ENUM to include 'processing' and set the new default DB::statement("ALTER TABLE assets MODIFY COLUMN status ENUM('active', 'processing', 'inactive') DEFAULT 'processing'"); // Optional: Update existing records to use the new status // DB::table('assets')->where('status', 'active')->update(['status' => 'processing']); } public function down() { // Revert the ENUM and default value DB::statement("ALTER TABLE assets MODIFY COLUMN status ENUM('active', 'processing', 'inactive') DEFAULT 'active'"); // Optional: Revert records if you updated them earlier // DB::table('assets')->where('status', 'processing')->update(['status' => 'active']); }
For PostgreSQL
If you're using PostgreSQL, the syntax is simpler (PostgreSQL handles ENUMs more cleanly out of the box):
public function up() { DB::statement("ALTER TABLE assets ALTER COLUMN status SET DEFAULT 'processing'"); // Optional: Update existing records // DB::table('assets')->where('status', 'active')->update(['status' => 'processing']); } public function down() { DB::statement("ALTER TABLE assets ALTER COLUMN status SET DEFAULT 'active'"); // Optional: Revert records // DB::table('assets')->where('status', 'processing')->update(['status' => 'active']); }
Step 3: Run the Migration
Finally, execute the migration to apply the changes:
php artisan migrate
Quick Tips
- Always back up your database before running migrations in production—better safe than sorry.
- Think carefully before updating existing records: some use cases might want to keep old entries as 'active' and only apply the new default to future inserts.
内容的提问来源于stack exchange,提问作者WaduNoop

