-
Notifications
You must be signed in to change notification settings - Fork 1
prevent premature query with no_autoflush to catch name exception #26
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
Conversation
WalkthroughWrapped subcategory assignment in CategoryById.put with db.session.no_autoflush to prevent autoflush during fetch-and-assign of Subcategory records. Logic remains: resolve provided subcategory IDs, abort with 422 if any missing, then extend the category’s subcategories. No public API or error handling changes beyond the autoflush context. Changes
Sequence Diagram(s)Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
9c21192 to
599fd3b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
app/routes/category.py (1)
101-107: Consider extracting subcategory validation logic.The subcategory validation logic (lines 101-107) is nearly identical to the POST method's validation (lines 56-61). Consider extracting this into a helper method to reduce duplication and improve maintainability.
Example refactor:
@staticmethod def _validate_and_fetch_subcategories(sc_ids): """Validate and fetch subcategories by IDs.""" subcategories = Subcategory.query.filter(Subcategory.id.in_(sc_ids)).all() if len(subcategories) != len(sc_ids): abort(422, message="One or more subcategories not present") return subcategoriesThen use in both POST and PUT:
if sc_ids := data.get("subcategories"): subcategories = CategoryCollection._validate_and_fetch_subcategories(sc_ids) category.subcategories = subcategories # or extend, after fixing the PUT semantics issue
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
app/routes/category.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
app/routes/category.py (1)
app/models.py (1)
Subcategory(93-110)
🔇 Additional comments (1)
app/routes/category.py (1)
100-107: Good use ofno_autoflushto defer integrity checks.The
no_autoflushcontext correctly prevents premature flushing during the subcategory query and assignment. Whencategory.nameis modified (line 98), the category becomes dirty and could trigger an autoflush during the subcategory query (lines 102-104), causing a name uniqueness violation to be raised before reaching the try/except block. By deferring the flush until the explicit commit (line 110), allIntegrityErrorcases are properly handled in one place.
Summary by CodeRabbit