feat(repository): add composite primary key support#640
Open
cofin wants to merge 15 commits intolitestar-org:mainfrom
Open
feat(repository): add composite primary key support#640cofin wants to merge 15 commits intolitestar-org:mainfrom
cofin wants to merge 15 commits intolitestar-org:mainfrom
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #640 +/- ##
==========================================
- Coverage 81.17% 80.70% -0.47%
==========================================
Files 99 99
Lines 7990 8159 +169
Branches 1079 1116 +37
==========================================
+ Hits 6486 6585 +99
- Misses 1180 1244 +64
- Partials 324 330 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
345f3c8 to
ef6a288
Compare
0213b6b to
6835f05
Compare
9f0fcab to
1e6808b
Compare
5acfbbd to
e4e8559
Compare
Adds support for composite (multi-column) primary keys in the repository layer. Changes: - Add PrimaryKeyType type alias supporting scalar, tuple, and dict formats - Add helper methods for composite key handling: - _is_composite_pk(): Check if model has composite PK - _build_pk_filter(): Build WHERE clause for PK lookup - _extract_pk_value(): Extract PK value(s) from instance - _pk_values_present(): Check if all PK values are set - _normalize_pk_values_to_tuples(): Convert PK values to tuples for bulk ops - Update get() to support composite keys (tuple or dict input) - Update delete() to support composite keys - Update delete_many() to use tuple_().in_() for efficient bulk operations - Cache PK columns in __init__ for performance The implementation follows SQLAlchemy's native patterns and maintains full backward compatibility for single-column primary keys. Closes litestar-org#189
Extend composite PK support from repository to service layer and memory repositories to complete Phase 3.1 and 3.2 of the composite primary key feature. Changes: - Update service layer get/delete/delete_many signatures to use PrimaryKeyType - Add composite PK helpers to memory repository (_pk_columns, _is_composite_pk, etc.) - Update memory repository get/delete/delete_many for composite keys - Add fallback in _build_pk_filter for mock objects without mapped PKs - Fix unit tests for new code paths Refs: litestar-org#189
Add explicit casts and type annotations to satisfy mypy and pyright strict mode for composite primary key handling methods. Changes: - Add ColumnElement[bool] casts for single-value PK comparisons - Use explicit type annotations instead of redundant casts - Extract type(pk_value).__name__ to local variable for type safety - Add guards for empty tuple edge cases in memory repository
- Replace cast() with explicit type annotations to avoid redundant-cast - Remove unnecessary `not isinstance(pk_value, str)` checks that mypy correctly identifies as unreachable (tuple/dict can't be str subclasses)
- Split combined isinstance check into separate checks to avoid type(pk_value).__name__ which triggers reportUnknownArgumentType - Add type: ignore[redundant-cast] for casts needed by pyright but flagged as redundant by mypy - Add reportUnknownArgumentType and reportUnknownVariableType = false to pyright config for consistency with existing disabled rules
…key validation into new utility functions.
…ods by removing redundant assignment.
…g in `_sync.py`.
…primary key handling in repositories and services.
…ynchronous service implementation.
MSSQL doesn't support tuple().in_() syntax for row value comparisons. Use OR of AND conditions as fallback for MSSQL dialect. Also fix mock repositories in unit tests to initialize _pk_columns and _pk_attr_names attributes required by composite PK support.
…ices, including a fix for MSSQL tuple().in_() syntax.
…documentation to use it, including minor heading style adjustment.
e4e8559 to
8485c21
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR adds support for composite (multi-column) primary keys throughout the repository and service layers. Now you can work with association tables, legacy databases with natural keys, or any model that uses multiple columns as its primary key.
What's Changed
Repository Layer
PrimaryKeyTypetype alias that accepts scalar values, tuples, or dicts_build_pk_filter(),_extract_pk_value(), etc.)get(),delete(), anddelete_many()now work with composite keystuple_().in_()for efficient bulk operations__init__for better performanceMSSQL Compatibility
WHERE (col1, col2) IN ((v1, v2), ...)Service Layer
get(),delete(), anddelete_many()signatures updated to accept composite keysMemory Repository
Documentation
CI Fix
Usage
Follow-up
update()andupsert()methods for composite key support (Phase 3.3)Closes #189