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.
Improved Joins Summary
Fixed
count
method now correctly counts Many-to-Many joined modelsAdded
count
method (passingjoins_config
)filters
inJoinConfig
)Detailed
JoinConfig
is a detailed configuration mechanism for specifying joins between models in FastCRUD queries. It contains the following key attributes:model
: The SQLAlchemy model to join.join_on
: The condition defining how the join connects to other models.join_prefix
: An optional prefix for the joined columns to avoid column name conflicts.schema_to_select
: An optional Pydantic schema for selecting specific columns from the joined model.join_type
: The type of join (e.g., "left", "inner").alias
: An optional SQLAlchemyAliasedClass
for complex scenarios like self-referential joins or multiple joins on the same model.filters
: An optional dictionary to apply filters directly to the joined model.Applying Joins in FastCRUD Methods
The
count
Method with JoinsThe
count
method can be enhanced with join operations to perform complex aggregate queries. Whilecount
primarily returns the number of records matching a given condition, introducing joins allows for counting records across related models based on specific relationships and conditions.Using
JoinConfig
For join requirements, the
count
method can be invoked with join parameters passed as a list ofJoinConfig
to thejoins_config
parameter:Fetching Data with
get_joined
andget_multi_joined
These methods are essential for retrieving records from a primary model while including related data from one or more joined models. They support both simple and complex joining scenarios, including self-referential joins and many-to-many relationships.
Simple Joins Using Base Parameters
For simpler join requirements, FastCRUD allows specifying join parameters directly:
model
: The target model to join.join_on
: The join condition.join_type
: Specifies the SQL join type.aliased
: WhenTrue
, uses an alias for the model in the join.join_prefix
: Optional prefix for columns from the joined model.filters
: Additional filters for the joined model.Examples of Simple Joining
Complex Joins Using
JoinConfig
When dealing with more complex join conditions, such as multiple joins, self-referential joins, or needing to specify aliases and filters,
JoinConfig
instances become the norm. They offer granular control over each join's aspects, enabling precise and efficient data retrieval.Many-to-Many Relationships with
get_multi_joined
FastCRUD simplifies dealing with many-to-many relationships by allowing easy fetch operations with joined models. Here, we demonstrate using
get_multi_joined
to handle a many-to-many relationship betweenProject
andParticipant
models, linked through an association table.Note on Handling Many-to-Many Relationships:
When using
get_multi_joined
for many-to-many relationships, it's essential to maintain a specific order in yourjoins_config
:This order ensures that the SQL joins are structured correctly to reflect the many-to-many relationship and retrieve the desired data accurately.
!!! TIP
Scenario
Imagine a scenario where projects have multiple participants, and participants can be involved in multiple projects. This many-to-many relationship is facilitated through an association table.
Models
Our models include
Project
,Participant
, and an association modelProjectsParticipantsAssociation
:Fetching Data with
get_multi_joined
To fetch projects along with their participants, we utilize
get_multi_joined
with appropriateJoinConfig
settings:Practical Tips for Advanced Joins
join_prefix
attribute to avoid column name collisions, especially in complex joins involving multiple models or self-referential joins.alias
attribute for disambiguating joins on the same model or for self-referential joins.filters
attribute inJoinConfig
to refine the data set returned by the query.JoinConfig
entries to ensure logical and efficient SQL join construction.Closes #33