'How to choose primary key to bind to source code properly
Let's suppose that I have a basic role table
CREATE TABLE IF NOT EXISTS myapp.role (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
In my application, I want to restrict somes part of code depending of role. I think about differents solutions but I am not sure what is the best (or if there is a better one).
A) I use an integer as primary key. In my application, i have a enum that is corresponding.
enum Role {
Admin = 1,
Moderator = 2,
Member = 3,
}
I use this to insert my seed data, and use this in code to check if my user has the right role.
B) I use a varchar primary key. In my application i have a union type corresponding.
type Role = 'ADMIN' | 'MODERATOR' | 'MEMBER';
C) I have a usual serial primary key, and an "identifier" column, then I need to cache or get the database state, and filter by identifier on my cached roles.
D) I don't know.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
