Skip to content

[Enum] Array of enum not nullable with default value #12388

@cavasinf

Description

@cavasinf

Bug Report

Q A
Version 2.20.9

Summary

Using a PHP backed enum with a Doctrine field mapped as SIMPLE_ARRAY (array of enums) breaks hydration when the column is non‑nullable and/or relies on a default value.
This leads to a runtime failure in Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator.

Attempting to solve this with a database-level default value also hits a MySQL 8.0 limitation. MySQL does not allow default values for TEXT columns (which SIMPLE_ARRAY maps to), and the generated migration therefore fails.
Even if manually adjusted, the next schema diff removes the fix and reintroduces the error.

Current behavior

#[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: false, enumType: MyEnum::class, options: ['default' => '[]'])]
private array $myEnums = [];

Hydration error:

Context: Trying to hydrate enum property "App\Entity\MyEntity::$myEnums"
Problem: Case "[]" is not listed in enum "App\Entity\MyEnum"
Solution: Either add the case to the enum type or migrate the database column to use another case of the enum

Expected behavior

Doctrine ORM should be able to hydrate entities that use Types::SIMPLE_ARRAY with enumType reliably on MySQL 8.0, including non‑nullable columns.

If Doctrine or Doctrine Migrations generates a default value for this column type, the generated SQL should be valid for MySQL 8.0.

Example MySQL error when the generated migration is executed:

Syntax error or access violation: 1101 BLOB, TEXT, GEOMETRY or JSON column 'equipement_groupes' can't have a default value

How to reproduce

  1. Use PHP 8.1+ with a backed enum:
enum MyEnum: string
{
    case A = 'A';
    case B = 'B';
}
  1. Create an entity with a non-nullable enum array property:
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Foo
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: Types::INTEGER)]
    private int $id;

   #[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: false, enumType: MyEnum::class, options: ['default' => '[]'])]
    private array $myEnums = [];
}
  1. Run schema/migration generation for MySQL 8.0 and apply it.
  2. Insert a row where my_enums is not defined to fallback to the default value
  3. Fetch the entity via ORM (e.g. repository find() / DQL query).
  4. Doctrine throws during hydration in: Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator

Extra debug

Nullable

    #[ORM\Column(type: Types::SIMPLE_ARRAY, enumType: MyEnum::class)]
    private array $myNonNullableEnums = [];

Works as expected, but NULL values are allowed.

NOT nullable

    #[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: false, enumType: MyEnum::class)]
    private array $myNonNullableEnums = [];
ALTER TABLE my_table ADD my_non_nullable_enums LONGTEXT NOT NULL COMMENT '(DC2Type:simple_array)',

When fetching from doctrine

Context: Trying to hydrate enum property "App\Entity\MyEntity::$myNonNullableWithDefaultEnums "
Problem: Case "" is not listed in enum "App\Entity\MyEnum"
Solution: Either add the case to the enum type or migrate the database column to use another case of the enum

NOT nullable with default value

    #[ORM\Column(type: Types::SIMPLE_ARRAY, nullable: false, enumType: MyEnum::class, options: ['default' => '[]'])]
    private array $myNonNullableWithDefaultEnums = [];

Generated migration:

ALTER TABLE my_table ADD my_non_nullable_with_default_enums LONGTEXT DEFAULT '[]' NOT NULL COMMENT '(DC2Type:simple_array)'

MySQL error:

SQLSTATE[42000]: Syntax error or access violation: 1101 BLOB, TEXT, GEOMETRY or JSON column 'equipement_groupes' can't have a default value

This requires manual modification of the migration.

- ALTER TABLE my_table ADD my_non_nullable_with_default_enums LONGTEXT DEFAULT '[]' NOT NULL COMMENT '(DC2Type:simple_array)'
+ ALTER TABLE my_table ADD my_non_nullable_with_default_enums LONGTEXT DEFAULT ('[]') NOT NULL COMMENT '(DC2Type:simple_array)'

Even after manually adjusting the migration, Doctrine still throws during hydration:

Context: Trying to hydrate enum property "App\Entity\MyEntity::$myNonNullableWithDefaultEnums "
Problem: Case "[]" is not listed in enum "App\Entity\MyEnum"
Solution: Either add the case to the enum type or migrate the database column to use another case of the enum

If the default value is an empty string instead of an empty array, the message changes slightly but the underlying issue remains: Problem: Case "" is not listed in enum "App\Entity\MyEnum"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions