Add return_keys support to upsert method and implement safe execution pattern for all CRUD operations#4
Merged
faizanazim11 merged 3 commits intomainfrom Sep 28, 2025
Conversation
…ll methods Co-authored-by: faizanazim11 <20454506+faizanazim11@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Update upsert method to support return_keys and safe execution for Table and Declarative objects
Add return_keys support to upsert method and implement safe execution pattern for all CRUD operations
Sep 28, 2025
faizanazim11
approved these changes
Sep 28, 2025
There was a problem hiding this comment.
Pull Request Overview
This PR adds return_keys parameter support to the upsert method and implements a safe execution pattern for all CRUD operations to ensure compatibility with both SQLAlchemy Table and Declarative objects.
- Added
return_keysparameter toupsertmethods in both sync and async versions - Refactored all CRUD operations to use safe attribute access pattern
getattr(table.c if isinstance(table, Table) else table, key) - Updated package version from 1.2.0 to 1.3.0
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| sql_db_utils/sql_utils.py | Added return_keys support to upsert method and implemented safe execution pattern for insert, update_with_where, and update methods |
| sql_db_utils/asyncio/sql_utils.py | Mirror implementation of sync version - added return_keys support to async upsert and safe execution patterns |
| sql_db_utils/version.py | Updated package version from 1.2.0 to 1.3.0 |
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.
This PR addresses an important compatibility issue with the
upsertmethod and inconsistent handling ofreturn_keysacross different table object types.Problem
The
upsertmethod was missing support for thereturn_keysparameter, which is available in other CRUD operations (insert,update,update_with_where,delete). Additionally, most methods usingreturn_keyshad an unsafe execution pattern that only worked with SQLAlchemy Table objects, failing with Declarative objects.Solution
1. Added
return_keysSupport toupsertMethodThe
upsertmethod now accepts areturn_keysparameter and returns the upserted data when specified:2. Implemented Safe Execution Pattern
All methods now use a safe pattern that works with both SQLAlchemy Table objects and Declarative objects:
This ensures compatibility across different SQLAlchemy usage patterns without breaking existing code.
Changes Made
return_keysparameter to both sync and asyncupsertmethodsinsert,update, andupdate_with_wheremethods to use the safe execution patternImpact
upsertmethod now supports returning inserted/updated datareturn_keyssupportThe implementation follows the existing patterns in the codebase and maintains the high code quality standards with proper error handling and logging.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.