How to Add a New Value to a PostgreSQL ENUM Column in Laravel

Use CaseLet’s say you have a 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

Login

To leave a reaction, you need to log in.

Comments

Michael Gough