Leonardo Herrera
Leonardo Herrera

Reputation: 8406

What is the best way to store enums in a database in Delphi?

Following the spirit of this question, what should be the ideal way to store enums in a database in Delphi? I have several lookup tables that guide some business logic, so a) they are tightly coupled with my code, and b) they may be subject to change in newer releases.

Right now I'm doing it by saving its numeric values, and setting the enum value explicitly.

type TSuit = (Unknown = 4, Heart = 1, Club = 3, Diamond = 2, Spade = 0);

Do you know a better way?

Upvotes: 4

Views: 566

Answers (1)

Ritsaert Hornstra
Ritsaert Hornstra

Reputation: 5111

Given the relational model you probably should store the value of the enumeration into the table at hand and create another table where the enum value + name are stored. The latter can be created purely from RTTI.

So given your example: you have something like a Card table where Suit is a byte field with values 0..4. And you have a Suits table with 5 records, one for each enum value.

Now your data is correctly normalized and the metadata is present you you know what Suit=2 means (eg join with the Suits table).

Upvotes: 3

Related Questions