Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing flags in a DB

In my application, I'd like the user to select his working days. then store them in the database. Of course my application will process the users' data like: Is today a working day for a specific user, who are the users that should work today , ... etc.

My question is, what is the best practice for doing this? should I use:

  1. Bitmasking field in the users table
  2. Many to many relationships tables by creating table for days, users and days_users. Thank you in advance.
like image 698
Gohary Avatar asked Dec 17 '22 18:12

Gohary


2 Answers

I would say that bit mask fields are a relational anti-pattern.

A field should have a single meaningful value, otherwise you end up with querying issues - parsing the field every time you need to query using it.

Such a field also requires extra documentation, as the values it stores are not self describing.

like image 169
Oded Avatar answered Jan 02 '23 07:01

Oded


Bitmasking field is a bit more cryptic in nature and you need to create something else to interpret what you store in the bitmask.

The second approach is a lot more transparent and easily understandable and it's a bit more flexible if you need to add more values. With the bitmask, you again need to redo your bitmap decoder each time you add a value which can be a maintainance nightmare compared to the relational approach.

like image 22
Jimmy Chandra Avatar answered Jan 02 '23 05:01

Jimmy Chandra