开发者

Is it a best practice to keep enums mirrored in the database as a form of documentation?

开发者 https://www.devze.com 2023-03-23 14:59 出处:网络
I have an enum in code enum EventType { Run = 1, Stop = 2, } 开发者_C百科 I have a table in my database

I have an enum in code

enum EventType
{
     Run = 1,
     Stop = 2,
}
开发者_C百科

I have a table in my database

CREATE TABLE [Event] (
    [Id] integer PRIMARY KEY AUTOINCREMENT,
    [EventType] integer NOT NULL,
    1679554756 text NOT NULL
);

Should I, if I don't really need to?

CREATE TABLE [EventType] (
    [EventTypeId] integer PRIMARY KEY AUTOINCREMENT,
    [Name] text
);
INSERT INTO "EventType" VALUES(1, "Run");
INSERT INTO "EventType" VALUES(2, "Stop");

This is bad because it's code duplication. It's good because it is a documentation for those who access the database and wonder what those EventType numbers mean. Should I drop these numbers altogether and use strings instead?


I would not create extra table just for the sake of documenting things. It increases overall complexity and therefore make it worse long term than having users wonder what those numbers are.

I, personally, like using string enums in DB exactly because it makes it self documenting. There could be different opinions, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消