Rodrigo
Rodrigo

Reputation: 3278

making the relationship through username and not id

So Lets say I have a user table and then a user_profile table. To connect the relationship there would be a user_id row within the user_profile table. Now when I'm building my applications I like to make my url show the username and not the user_id example:

http://www.example.com/username/profile

not:

http://www.example.com/user_id/profile

So what I find myself doing is getting the user_id through the username and then fetching the profile info which just adds an extra query for no reason. My questions is could I just make the relationship through the username which is just as unique as the id row in the users table. Or is this bad practice and I should stick with using user_id?

Upvotes: 2

Views: 104

Answers (1)

timclutton
timclutton

Reputation: 13004

A much better idea would be to use the user_id to join the tables, but provide the username as a parameter. Something like:

select p.*
  from users u
     , profiles p
 where u.id = p.user_id
   and u.username = ?

Upvotes: 2

Related Questions