Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Using aliases
Added
Detailed
In complex query scenarios, particularly when you need to join a table to itself or perform multiple joins on the same table for different purposes, aliasing becomes crucial. Aliasing allows you to refer to the same table in different contexts with unique identifiers, avoiding conflicts and ambiguity in your queries.
For both
get_joined
andget_multi_joined
methods, when you need to join the same model multiple times, you can utilize thealias
parameter within yourJoinConfig
to differentiate between the joins. This parameter expects an instance ofAliasedClass
, which can be created using thealiased
function from SQLAlchemy (also in fastcrud for convenience).Example: Joining the Same Model Multiple Times
Consider a task management application where tasks have both an owner and an assigned user, represented by the same
UserModel
. To fetch tasks with details of both users, we use aliases to join theUserModel
twice, distinguishing between owners and assigned users.Let's start by creating the aliases and passing them to the join configuration. Don't forget to use the alias for
join_on
:Then just pass this joins_config to
get_multi_joined
:In this example,
owner_alias
andassigned_user_alias
are created fromUserModel
to distinguish between the task's owner and the assigned user within the task management system. By using aliases, you can join the same model multiple times for different purposes in your queries, enhancing expressiveness and eliminating ambiguity.Closes #27