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"
2
Answers
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.
The UID never changes and is generally used to reference related data in other collections/tables over username (as users are usually allowed to change their names in most of the applications). You have not provided many details on your applications but these are handy for most of the actions such as:
I am not sure about which database you use, but as long as you have proper indexes setup, the queries should be fast. It’s pretty common to use UUIDs or MongoBD’s ObjectIDs as user IDs.