Multiple foreign-keys to the same Model

When there are multiple foreign keys to the same model, it is good practice toexplicitly specify which field you are joining on.

Referring back to the example app’s models,consider the Relationship model, which is used to denote when one userfollows another. Here is the model definition:

  1. class Relationship(BaseModel):
  2. from_user = ForeignKeyField(User, backref='relationships')
  3. to_user = ForeignKeyField(User, backref='related_to')
  4.  
  5. class Meta:
  6. indexes = (
  7. # Specify a unique multi-column index on from/to-user.
  8. (('from_user', 'to_user'), True),
  9. )

Since there are two foreign keys to User, we should always specify whichfield we are using in a join.

For example, to determine which users I am following, I would write:

  1. (User
  2. .select()
  3. .join(Relationship, on=Relationship.to_user)
  4. .where(Relationship.from_user == charlie))

On the other hand, if I wanted to determine which users are following me, Iwould instead join on the from_user column and filter on the relationship’sto_user:

  1. (User
  2. .select()
  3. .join(Relationship, on=Relationship.from_user)
  4. .where(Relationship.to_user == charlie))