Enums saved to database. How

Recently we ended up in an issue with enums. One of the developers changed the order of enums because of a change he had to do. And he thought that enum values that are logically together should be put together in enum class also.

But when he was done with the change, all the tests related to eums started to fail. Thankfully we had test checking exactly this case of enum order change.

Now the question is what happens with enum order change. On the outside it simply looks a harmless change.

Enum ordinal is saved to the database as an int rather than the enum name. So, You will get away with the name change of enum value but not the order change.

So the value in enum doesn’t represent the enum stored in database. But the position it is at in the enum list.

So if the enums are saved to database, be careful with changing the order in enums. And add some tests to detect the changes in enum order. Otherwise in production you may end up with wiered issues.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.