Skip to content
OpenCloseEsc
Tips
  • Laravel
  • database
  • enums
  • migrations

A plain string column, not a MySQL enum

Adding a case to a MySQL ENUM column is an ALTER TABLE that can lock a big table for minutes. A string column with an index stores the same values, and the PHP enum does the checking.

1 min read

The database does not need to know the allowed values when the application already does.

$table->string('status', 32)->default(OrderStatus::Pending->value);
$table->index('status');

A new case is one line in the enum and no migration. With $table->enum('status', [...]) the same change rewrites the column, and on a large table the rewrite holds a lock while it runs.

Validation stays where it is: Rule::enum() on the way in, the enum cast on the way out. Clean existing rows first (SELECT DISTINCT status FROM orders), because a value the enum does not know makes the model throw when it is read.