Skip to main content
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

The ALTER is accepted, but the row stored with the number 1 can no longer be shown:
Several checks still look healthy, so the problem can go unnoticed:
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.
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. 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:
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:
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:
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:

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:
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):
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

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: