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

Fermin Perdomo Fermin Perdomo
schedule 1 min read
How to Add a New Value to a PostgreSQL ENUM Column in Laravel

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.

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

Reactions

lock You need to be logged in to react.
Log In

Newsletter

Get new posts delivered straight to your inbox.

mail

Great Tools for Developers

Git Tower

Git Tower

A powerful Git client for Mac and Windows that simplifies version control.

Visit arrow_forward
Mailcoach

Mailcoach

Self-hosted email marketing platform for sending newsletters and automated emails.

Visit arrow_forward
Uptimia

Uptimia

Website monitoring and performance testing tool to ensure your site is always up and running.

Visit arrow_forward
Cloudways

Cloudways

Managed cloud hosting platform that simplifies server management for developers.

Visit arrow_forward

Comments

No comments yet. Be the first to share your thoughts.

chat_bubble Join the conversation — log in to leave a comment.
Log In