diff --git a/docs/tutorial/relationship-attributes/aliased-relationships.md b/docs/tutorial/relationship-attributes/aliased-relationships.md index a614fa8ef6..79e5bdf2c3 100644 --- a/docs/tutorial/relationship-attributes/aliased-relationships.md +++ b/docs/tutorial/relationship-attributes/aliased-relationships.md @@ -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` @@ -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 @@ -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