3

Is it a major flaw if I'm querying my user's information by their user_id (string) rather than creating a Profile model and linking them to other models using SQL relationships?

Example 1: (user_id is stored in django sessions.)

class Information(models.Model):
    user_id = models.CharField(...)
    ...

# also applies for .filter() operations.
information = Information.objects.get(user_id=request.getUser['user_id'])

note: I am storing the user's profile informations on Auth0.

Example 2: (user_id is stored in Profile.)

class Profile(models.Model):
    user_id = models.CharField(...)

class Information(models.Model):
    profile = models.ForeginKey(Profile, ...)
    ...

information = Information.objects.get(profile=request.getProfile)

note: With this method Profile will only have one field, user_id.

On Django, will using a string instead of a query object affect performances to retrieve items?

Endre Both
  • 5,540
  • 1
  • 26
  • 31
Horai Nuri
  • 5,358
  • 16
  • 75
  • 127
  • 1
    As long as the field you query is indexed (note that only pk and fk fields are indexed by default, so you need to add the index for the `CharField` yourself), there's no difference, because the query you make is on an indexed field of the `Information` table. But not having a fk means you have to make sure the user id's you store are unique and indeed represent objects in your db (if they are supposed to be the id of other objects in your db). – dirkgroten Apr 11 '19 at 15:35
  • I may have misunderstood your concept; I'm not clear about the information flow between Auth0 and your application. Does `Information` contain all user information that you store in your own database? If so, than option 2 appears redundant. As Dirk noted, performance is a non-issue. – Endre Both Apr 11 '19 at 20:16
  • @EndreBoth Indeed, still your answer opens a new perspective on what I should and should not put into an external storage. Since performance isn't an issue, that solves my question. I will gladly accept your answer if you include the performance bit into it. – Horai Nuri Apr 11 '19 at 20:36

1 Answers1

4

Performance is not an issue here as noted by Dirk; as soon as a column is indexed, the performance difference between data types should be negligible when compared to other factors. Here's a related SO question for more perspective.

What you should take care of is to prevent the duplication of data whose integrity you then would have to take care of on your own instead of relying on well-tested integrity checks in the database.

Another aspect is that if you do have relations between your data, you absolutely should make sure that they are accurately represented in your models using Django's relationships. Otherwise there's really not much point in using Django's ORM at all. Good luck!

Endre Both
  • 5,540
  • 1
  • 26
  • 31
  • In the first case, duplicated data is managed by the Auth0 API (user_id, email, etc...), but I get your point, it'd require a bit more coding to reverse filter emails for instance. – Horai Nuri Apr 11 '19 at 15:40
  • 1
    Great input, but not really answering the performance question. – Marcus Lind Apr 11 '19 at 16:03