Skip to content

Commit

Permalink
馃帹 [pre-commit.ci] Auto format from pre-commit.com hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Feb 24, 2024
1 parent da7c0c3 commit 9606fac
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions docs/tutorial/relationship-attributes/aliased-relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

## Multiple Relationships to the Same Model

We've seen how tables are related to each other via a single relationship attribute but what if more than
one attribute links to the same table?
We've seen how tables are related to each other via a single relationship attribute but what if more than
one attribute links to the same table?

What if you have a `User` model and an `Address` model and would like
to have `User.home_address` and `User.work_address` relationships to the same
to have `User.home_address` and `User.work_address` relationships to the same
`Address` model? In SQL you do this by creating a table alias using `AS` like this:

```
SELECT *
FROM user
SELECT *
FROM user
JOIN address AS home_address_alias
ON user.home_address_id == home_address_alias.id
JOIN address AS work_address_alias
ON user.work_address_id == work_address_alias.id
```

The aliases we create are `home_address_alias` and `work_address_alias`. You can think of them
The aliases we create are `home_address_alias` and `work_address_alias`. You can think of them
as a view to the same underlying `address` table.

We can this with **SQLModel** and **SQLAlchemy** using `sqlalchemy.orm.aliased`
Expand All @@ -45,7 +45,7 @@ winter and summer teams or on the same team for both seasons.

///

The `sa_relationship_kwargs={"primaryjoin": ...}` is a new bit of info we need for **SQLAlchemy** to
The `sa_relationship_kwargs={"primaryjoin": ...}` is a new bit of info we need for **SQLAlchemy** to
figure out which SQL join we should use depending on which attribute is in our query.

## Creating Heros
Expand All @@ -71,7 +71,7 @@ team to the `winter_team` and `summer_team` attributes:
///
## Searching for Heros

Querying `Heros` based on the winter or summer teams adds a bit of complication. We need to create the
Querying `Heros` based on the winter or summer teams adds a bit of complication. We need to create the
alias and we also need to be a bit more explicit in how we tell **SQLAlchemy** to join the `hero` and `team` tables.

We create the alias using `sqlalchemy.orm.aliased` function and use the alias in the `where` function. We also
Expand Down

0 comments on commit 9606fac

Please sign in to comment.