Home > Back-end >  Is there an advantage of having a userId AND a username even if they're both strings?
Is there an advantage of having a userId AND a username even if they're both strings?

Time:11-06

I have come across different variations when it comes to user authentication and I have seen developers include a userId field AND a username field (both strings) in their database so I wondered if there is an advantage if you have both of these fields.

I would rather have only one field (username) to have a better overview and prevent possible mistakes but I'm not sure if there could be problems that come along with that variation.

I've already looked on the internet but only found a Quora answer which stated it would make sense to have a userId as an integer since "Integers are easier/faster/less expensive to sort, search, and compare than strings"

CodePudding user response:

The solution in the linked answer applies to SQL databases and not to NoSQL databases. If you're using Firebase Authentication, I cannot see any reason why would you store the UID or the user names in the database. Why? Because you can always get those values from the current user object without the need of performing a database call. As a general rule, always store in the database only the data that you need in your application code and nothing more.

  • Related