How to Add a New Value to a PostgreSQL ENUM Column in Laravel
Fermin Perdomo
If you're using PostgreSQL with Laravel and have a table column defined as an ENUM, you may run into a common issue: PostgreSQL does not let you modify ENUMs like regular fields. Instead, you need to alter the underlying ENUM type directly — which can be confusing at first.
Here’s how to properly add a new value to a PostgreSQL ENUM column in a Laravel project.
transactions table with a type column defined as an ENUM in PostgreSQL. Initially, it supports values like 'credit' and 'debit'.Now you want to add a new option: 'withdrawal'.Step 1: Create a Migration to Add a New ValueCreate a migration:php artisan make:migration add_withdrawal_to_transaction_type_enumInside the generated file:
use Illuminate\Database\Migrations\Migration; use Illuminate\Support\Facades\DB; class AddWithdrawalToTransactionTypeEnum extends Migration { public function up() { DB::statement('ALTER TABLE transactions DROP CONSTRAINT transactions_type_check;'); DB::statement('ALTER TABLE transactions ADD CONSTRAINT transactions_type_check CHECK (status::TEXT = ANY (ARRAY[\'holder\'::CHARACTER VARYING, \'referral\'::CHARACTER VARYING, \'withdrawal\'::CHARACTER VARYING]::TEXT[]))'); } public function down() { // PostgreSQL doesn't support removing enum values easily. // You’d have to recreate the type if you really needed to revert. } }Run the migration:
php artisan migrate
Newsletter
Get new posts delivered straight to your inbox.
Great Tools for Developers
Git Tower
Get Started - It's FreeA powerful Git client for Mac and Windows that simplifies version control.
Mailcoach
Start freeSelf-hosted email marketing platform for sending newsletters and automated emails.
Uptimia
Start freeWebsite monitoring and performance testing tool to ensure your site is always up and running.
Cloudways
Start freeManaged cloud hosting platform that simplifies server management for developers.
Comments
No comments yet. Be the first to share your thoughts.