Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to secondary tables relationships #218

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

Ckk3
Copy link
Contributor

@Ckk3 Ckk3 commented Nov 23, 2024

Fixes #19

Description

This PR fixes the relationships involving secondary tables by primarily updating the logic in loader.py.

Notably, a new flag, disabled_optimization_to_secondary_tables, has been introduced in the self._scalars_all function. This flag is required to disable SQLAlchemy's default optimization, which retrieves only the related_model values. In our case, this optimization must be disabled to retrieve values for both the self_model and the related_model.

This adjustment is necessary because the keys variable is used to match data from both the self_model and the related_model. Please review the implementation of this flag and its impact on the overall query behavior.

IMPORTANT
During development, I discovered an issue that causes the ModelConnection type to be duplicated when we have relationship. After investigation, it seems this problem is related to the Relay implementation rather than the secondary table logic.

To address this, I will create a separate issue. I’ve included a failing test in this PR, test_query_with_secondary_table_with_values_list, which demonstrates the issue.

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

Summary by Sourcery

Add support for secondary table relationships in the SQLAlchemy mapper, addressing a bug and enhancing the loader to handle these relationships efficiently. Include comprehensive tests to ensure the correct implementation of these features.

New Features:

  • Introduce support for querying secondary table relationships in the database schema.

Bug Fixes:

  • Fix an issue related to the handling of secondary table relationships in the SQLAlchemy mapper.

Enhancements:

  • Refactor the SQLAlchemy loader to handle secondary table relationships more efficiently by disabling certain optimizations when necessary.

Tests:

  • Add extensive tests to verify the correct functionality of secondary table relationships, including scenarios with different foreign keys and multiple secondary tables.

@Ckk3 Ckk3 self-assigned this Nov 23, 2024
Copy link
Contributor

sourcery-ai bot commented Nov 23, 2024

Reviewer's Guide by Sourcery

This PR adds support for secondary tables relationships in SQLAlchemy mapper by modifying how relationships with secondary tables are handled in the loader and mapper components. The changes primarily focus on adjusting the query construction and data retrieval logic to properly handle relationships that use junction/association tables.

ER diagram for Employee and Department with secondary table

erDiagram
    EMPLOYEE {
        int id PK
        string name
        string role
    }
    DEPARTMENT {
        int id PK
        string name
    }
    EMPLOYEE_DEPARTMENT_JOIN_TABLE {
        int employee_id PK
        int department_id PK
    }
    EMPLOYEE ||--o{ EMPLOYEE_DEPARTMENT_JOIN_TABLE : ""
    DEPARTMENT ||--o{ EMPLOYEE_DEPARTMENT_JOIN_TABLE : ""
Loading

ER diagram for Employee, Department, and Building with secondary tables

erDiagram
    EMPLOYEE {
        int id PK
        string name
        string role
    }
    DEPARTMENT {
        int id PK
        string name
    }
    BUILDING {
        int id PK
        string name
    }
    EMPLOYEE_DEPARTMENT_JOIN_TABLE {
        int employee_id PK
        int department_id PK
    }
    EMPLOYEE_BUILDING_JOIN_TABLE {
        int employee_id PK
        int building_id PK
    }
    EMPLOYEE ||--o{ EMPLOYEE_DEPARTMENT_JOIN_TABLE : ""
    DEPARTMENT ||--o{ EMPLOYEE_DEPARTMENT_JOIN_TABLE : ""
    EMPLOYEE ||--o{ EMPLOYEE_BUILDING_JOIN_TABLE : ""
    BUILDING ||--o{ EMPLOYEE_BUILDING_JOIN_TABLE : ""
Loading

File-Level Changes

Change Details Files
Modified relationship key handling for secondary tables
  • Added special case handling for secondary table relationships when getting relationship keys
  • Updated relationship key generation to use only the first ID for secondary tables
  • Added condition to check for secondary table presence in relationship handling
src/strawberry_sqlalchemy_mapper/mapper.py
Enhanced loader functionality for secondary table relationships
  • Added new query construction logic for relationships with secondary tables
  • Implemented custom data grouping for secondary table relationships
  • Added disabled_optimization_to_secondary_tables parameter to control SQLAlchemy optimization
  • Modified _scalars_all method to support secondary table queries
src/strawberry_sqlalchemy_mapper/loader.py
Added comprehensive test coverage for secondary table relationships
  • Added tests for basic secondary table relationships
  • Added tests for relationships with multiple secondary tables
  • Added tests for secondary tables with different foreign keys
  • Added tests for secondary tables with uselist=False
tests/relay/test_connection.py

Assessment against linked issues

Issue Objective Addressed Explanation
#19 Fix SQLAlchemy relationships that fail when using secondary tables

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov-commenter
Copy link

codecov-commenter commented Nov 23, 2024

Codecov Report

Attention: Patch coverage is 92.24953% with 41 lines in your changes missing coverage. Please review.

Project coverage is 87.03%. Comparing base (2133fd7) to head (0fb61bb).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #218      +/-   ##
==========================================
+ Coverage   85.51%   87.03%   +1.51%     
==========================================
  Files          16       16              
  Lines        1629     2105     +476     
  Branches      139      148       +9     
==========================================
+ Hits         1393     1832     +439     
- Misses        173      207      +34     
- Partials       63       66       +3     
---- 🚨 Try these New Features:

Copy link

codspeed-hq bot commented Nov 23, 2024

CodSpeed Performance Report

Merging #218 will not alter performance

Comparing Ckk3:issue-19 (0fb61bb) with main (2133fd7)

Summary

✅ 1 untouched benchmarks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQLAlchemy Relationships fail when using secondary Table
2 participants