strawberry-sqlalchemy icon indicating copy to clipboard operation
strawberry-sqlalchemy copied to clipboard

Add support to secondary tables relationships

Open Ckk3 opened this issue 1 year ago • 11 comments

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

  • [x] Core
  • [x] Bugfix
  • [ ] New feature
  • [ ] Enhancement/optimization
  • [ ] Documentation

Issues Fixed or Closed by This PR

  • #19

Checklist

  • [x] My code follows the code style of this project.
  • [ ] My change requires a change to the documentation.
  • [ ] I have updated the documentation accordingly.
  • [x] I have read the CONTRIBUTING document.
  • [x] I have added tests to cover my changes.
  • [x] 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.

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 avatar Nov 23 '24 00:11 Ckk3