Persisting Enums in database tables

后端 未结 5 1288
攒了一身酷
攒了一身酷 2021-01-11 09:43

I have an order which has a status (which in code is an Enum). The question is how to persist this. I could:

  1. Persist the string in a field and then map back t
5条回答
  •  有刺的猬
    2021-01-11 10:36

    If this is a fixed list (which it seems it is, or else you shouldn't store it as an enum), I wouldn't use #1.

    The main reason to use #3 over #2 is for ease of use with self-service querying utilities. However, I'd actually go with a variant of #2: Store the value as an integer and map to an enum on data retrieval. However, also create a table representing the enum type, with the value as the PK and the name as another column. That way it's simple, quick, and efficient to use with your code, but also easy to get the logical value with self-service querying and other uses that don't use your data access code.

提交回复
热议问题