> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Unexpected value in enum (UNKNOWN_ELEMENT_OF_ENUM) after changing Enum values with ALTER

> Why reading an Enum column fails with error 691 after an ALTER changed the number of an existing value, how to make the data readable again, and how to renumber safely.

The error `Unexpected value N in enum` (code 691, `UNKNOWN_ELEMENT_OF_ENUM`) means that the column contains the number `N`, and the current `Enum` definition has no name for it. The usual cause is an `ALTER TABLE ... MODIFY COLUMN` that changed the number of an existing member or removed a member that is used. An `Enum` column stores the numbers, and such an `ALTER` changes the definition without converting the stored numbers. No data is lost: give every stored number a name again and the column is readable.

## Example

```sql theme={null}
CREATE TABLE orders
(
    id UInt32,
    status Enum8('new' = 1, 'shipped' = 2, 'cancelled' = 3)
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO orders VALUES (1, 'new'), (2, 'shipped'), (3, 'cancelled');

ALTER TABLE orders MODIFY COLUMN status Enum8('new' = 9, 'shipped' = 2, 'cancelled' = 3);
```

The `ALTER` is accepted, but the row stored with the number 1 can no longer be shown:

```sql theme={null}
SELECT * FROM orders ORDER BY id;
```

```text theme={null}
Code: 691. DB::Exception: Unexpected value 1 in enum. (UNKNOWN_ELEMENT_OF_ENUM)
```

Several checks still look healthy, so the problem can go unnoticed:

```sql theme={null}
SELECT count() FROM orders;

SELECT command, is_done, latest_fail_reason
FROM system.mutations
WHERE database = currentDatabase() AND table = 'orders';
```

```text theme={null}
   ┌─count()─┐
1. │       3 │
   └─────────┘
   ┌─command───────────────────────────────────────────────────────────────────┬─is_done─┬─latest_fail_reason─┐
1. │ (MODIFY COLUMN `status` Enum8('new' = 9, 'shipped' = 2, 'cancelled' = 3)) │       1 │                    │
   └───────────────────────────────────────────────────────────────────────────┴─────────┴────────────────────┘
```

Queries that don't read the column work, the mutation started by the `ALTER` is reported as done, and a filter by the renumbered name returns no rows instead of failing: `SELECT id FROM orders WHERE status = 'new'` returns an empty result here.

> [!NOTE]
>
> ClickHouse rejects some of these statements with `CANNOT_CONVERT_TYPE` and a message like `Enum conversion changes value for element 'shipped' from 2 to 20`, but the check doesn't catch every case, as the example shows. This is tracked in [issue #116182](https://github.com/ClickHouse/ClickHouse/issues/116182). Don't rely on the check.

## Find out which numbers are stored

Converting the column to its numeric type works even when the names can't be resolved. Use `toInt8` for `Enum8` and `toInt16` for `Enum16`:

```sql theme={null}
SELECT
    toInt8(status) AS stored_value,
    count() AS rows
FROM orders
GROUP BY stored_value
ORDER BY stored_value;

SELECT type
FROM system.columns
WHERE database = currentDatabase() AND table = 'orders' AND name = 'status';
```

```text theme={null}
   ┌─stored_value─┬─rows─┐
1. │            1 │    1 │
2. │            2 │    1 │
3. │            3 │    1 │
   └──────────────┴──────┘
   ┌─type─────────────────────────────────────────────┐
1. │ Enum8('shipped' = 2, 'cancelled' = 3, 'new' = 9) │
   └──────────────────────────────────────────────────┘
```

Every stored number that is missing from the type is a number that fails on read.

## Make the column readable again

If no rows were inserted after the change, restore the previous definition:

```sql theme={null}
ALTER TABLE orders MODIFY COLUMN status Enum8('new' = 1, 'shipped' = 2, 'cancelled' = 3);

SELECT * FROM orders ORDER BY id;
```

```text theme={null}
   ┌─id─┬─status────┐
1. │  1 │ new       │
2. │  2 │ shipped   │
3. │  3 │ cancelled │
   └────┴───────────┘
```

Rows inserted after the change are stored with the new number, so the previous definition would make those rows unreadable. In that case the query above shows both numbers (1 and 9 in this example). Use a definition that names both, then merge them:

```sql theme={null}
ALTER TABLE orders
    MODIFY COLUMN status Enum8('new' = 1, 'shipped' = 2, 'cancelled' = 3, 'new_renumbered' = 9);

ALTER TABLE orders UPDATE status = 'new' WHERE status = 'new_renumbered';
```

When the `UPDATE` mutation has finished and no row uses the temporary name, you can remove it from the definition.

Like the original change, each of these `ALTER TABLE ... MODIFY COLUMN` statements starts a mutation that rewrites the column in existing parts.

If such an `ALTER` is rejected with `CANNOT_CONVERT_TYPE`, you can still read the data by number and copy it into a new table. A cast of the stored number to a definition that names every number works in a `SELECT`:

```sql theme={null}
SELECT
    id,
    toInt8(status)::Enum8('new' = 1, 'shipped' = 2, 'cancelled' = 3, 'new_renumbered' = 9) AS status
FROM orders;
```

## Change the number of a member safely

Convert through the name, not through the number. One way is to change the column to `String` and back. Both steps are mutations that rewrite the column:

```sql theme={null}
ALTER TABLE orders MODIFY COLUMN status String;
ALTER TABLE orders MODIFY COLUMN status Enum8('new' = 9, 'shipped' = 2, 'cancelled' = 3);

SELECT
    id,
    status,
    toInt8(status) AS stored_value
FROM orders
ORDER BY id;
```

```text theme={null}
   ┌─id─┬─status────┬─stored_value─┐
1. │  1 │ new       │            9 │
2. │  2 │ shipped   │            2 │
3. │  3 │ cancelled │            3 │
   └────┴───────────┴──────────────┘
```

The other way is to copy the data into a new table and swap the tables. The `toString` function returns the name of an `Enum` value, and the name is resolved against the definition of the target column. This is the only way when the column is part of the sorting key, because such a column can't be converted to `String` in place (the `ALTER` fails with `ALTER_OF_COLUMN_IS_FORBIDDEN`):

```sql theme={null}
CREATE TABLE orders_new
(
    id UInt32,
    status Enum8('new' = 1, 'shipped' = 20, 'cancelled' = 3)
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO orders_new
SELECT
    id,
    toString(status)
FROM orders;

EXCHANGE TABLES orders AND orders_new;
```

Stop the writes to the table before the copy and resume them after `EXCHANGE TABLES`. The copy sees the rows that exist when it starts, and the swap does not move later rows: a row inserted in between ends up only in the old table, which now has the name `orders_new`.

## Which changes are safe

| Change to an `Enum` column | Result |
| - | - |
| Add a new member | Safe. Only the table definition changes, parts are not rewritten. |
| Remove a member that was never written | Safe. Runs a mutation. |
| Remove a member that exists in the data | Accepted, then reads of those rows fail with `UNKNOWN_ELEMENT_OF_ENUM`. |
| Change the number of an existing member | Rejected with `CANNOT_CONVERT_TYPE`, or accepted with the result shown above. |
| `Enum8` to `Enum16` or back, same numbers | Safe. Runs a mutation that rewrites the column. |

The practical rule is to only add members to an `Enum` of a table that holds data. Since ClickHouse 26.6 you can add members without repeating the whole definition. The number is optional and defaults to the next free one, so `'returned'` gets the number 22 here:

```sql theme={null}
ALTER TABLE orders MODIFY COLUMN status ADD ENUM VALUES ('refunded' = 21, 'returned');
```

## Related pages

* [`Enum` data type](https://clickhouse.com/docs/reference/data-types/enum)
* [`MODIFY COLUMN ADD ENUM VALUES`](https://clickhouse.com/docs/reference/statements/alter/column#modify-column-add-enum-values)
* [`system.mutations`](https://clickhouse.com/docs/reference/system-tables/mutations)
