Keeping an enum and a table in sync

Posted by MPelletier on Programmers See other posts from Programmers or by MPelletier
Published on 2012-11-28T19:06:31Z Indexed on 2012/11/29 11:20 UTC
Read the original article Hit count: 393

Filed under:
|

I'm making a program that will post data to a database, and I've run into a pattern that I'm sure is familiar: A short table of most-likely (very strongly likely) fixed values that serve as an enum. So suppose the following table called Status:

  Status
  Id Description
  --------------
   0 Unprocessed
   1 Pending
   2 Processed
   3 Error

In my program I need to determine a status Id for another table, or possibly update a record with a new status Id.

I could hardcode the status Id's in an enum and hope no one ever changes the database. Or I could pre-fetch the values based on the description (thus hardcoding that instead).

What would be the correct approach to keep these two, enum and table, synced?

© Programmers or respective owner

Related posts about database

Related posts about enum