Skip to content

Improve validation and failure modes in with_parent() - #13586

Open
VimalN2005 wants to merge 1 commit into
sqlalchemy:mainfrom
VimalN2005:fix-6860-with-parent-validation
Open

VimalN2005 wants to merge 1 commit into
sqlalchemy:mainfrom
VimalN2005:fix-6860-with-parent-validation

Conversation

@VimalN2005

Copy link
Copy Markdown

Fixes

Fixes: #6860

Description

Improve error reporting and validation for with_parent():

  • Added early validation in with_parent() ensuring instance is not None and is a mapped instance, raising a descriptive ArgumentError.
  • Added early validation in _with_parent() and _optimized_compare() ensuring instance matches the parent mapper of the provided relationship property (not state.mapper.isa(mapper)), raising an ArgumentError when mismatched entities are passed (e.g., with_parent(a1, B.a) where B.a belongs to B).
  • Added validation for property to ensure it is a valid relationship attribute or queryable property, raising ArgumentError rather than falling through to an unhandled AttributeError.

Checklist

  • Added unit tests covering all failure modes in test/orm/test_query.py (ParentTest)
  • All 19 ParentTest tests and existing with_parent tests pass cleanly
  • Added changelog fragment doc/build/changelog/unreleased_21/6860.rst

Add early validation in with_parent() and _with_parent() to ensure:
- instance is a mapped instance and not None
- prop is a valid relationship property or queryable attribute
- instance is compatible with the parent mapper of the relationship

Fixes: sqlalchemy#6860
@sqla-tester

Copy link
Copy Markdown
Collaborator

Thanks! Issue #6860 is now marked code review in progress and no longer open for pull requests, so this pull request holds the review for it and another one won't land on top of your work. If this pull request is abandoned, a maintainer can put open for pull requests back on #6860 to reopen it to others.

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.

smoother with_parent() failure modes

2 participants