fix(policy): wrong table alias used when injecting for field policies#2413
fix(policy): wrong table alias used when injecting for field policies#2413
Conversation
📝 WalkthroughWalkthroughA one-line fix in the policy handler changes model-level policy filter construction to use the model name instead of the alias parameter, preventing alias scoping errors in nested relations. A regression test validates the fix handles relation includes with static-value access policies. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
|
Claude finished @ymc9's task —— View job Code Review for PR #2413Reviewing: fix(policy): wrong table alias used when injecting for field policies Review Tasks
Analysis SummaryThis PR fixes a critical table alias bug in field policy injection that was causing SQL generation errors when using field-level policies with includes/joins. Code Quality ✅Fix Implementation ( - const modelPolicyFilter = this.buildPolicyFilter(model, alias, operation);
+ const modelPolicyFilter = this.buildPolicyFilter(model, model, operation);Strengths:
Test Coverage ✅Regression Test (
Potential Issues & RecommendationsMinor Concerns:
Security ✅
Performance ✅
Bug Analysis ✅Root Cause: When Impact: This bug would manifest as "missing FROM-clause entry" database errors when:
Fix Quality: The fix correctly identifies that within this method context, the model name itself should be used as the table reference for policy filters, not the potentially undefined alias. Overall Assessment: ✅ APPROVEDThis is a well-executed bug fix with:
The fix correctly resolves the table alias issue in field policy injection and should prevent the "missing FROM-clause entry" errors in complex queries with includes and field-level policies. ** |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/regression/test/issue-2385.test.ts (1)
47-51: Strengthen the assertion to validate returned data, not only non-throw behavior.This currently proves “no exception,” but it can still pass with an empty result set. Assert row and included relation counts to make the regression stricter.
Suggested test assertion update
- await expect( - userDb.user.findMany({ - include: { associations: true }, - }), - ).toResolveTruthy(); + const users = await userDb.user.findMany({ + include: { associations: true }, + }); + + expect(users).toHaveLength(1); + expect(users[0]?.associations).toHaveLength(1);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/regression/test/issue-2385.test.ts` around lines 47 - 51, The test currently only asserts that userDb.user.findMany with include: { associations: true } does not throw; instead capture the result into a variable (e.g., const users = await userDb.user.findMany(...)) and assert the returned data contents: check users.length > 0 to ensure rows were returned and verify the included relation(s) are present by asserting each item has an associations array (e.g., users.every(u => Array.isArray(u.associations))) and optionally that at least one user has associations.length > 0 to validate relations were loaded; update the expectation in the test accordingly (replace the toResolveTruthy() check with explicit assertions against the users variable).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/regression/test/issue-2385.test.ts`:
- Around line 47-51: The test currently only asserts that userDb.user.findMany
with include: { associations: true } does not throw; instead capture the result
into a variable (e.g., const users = await userDb.user.findMany(...)) and assert
the returned data contents: check users.length > 0 to ensure rows were returned
and verify the included relation(s) are present by asserting each item has an
associations array (e.g., users.every(u => Array.isArray(u.associations))) and
optionally that at least one user has associations.length > 0 to validate
relations were loaded; update the expectation in the test accordingly (replace
the toResolveTruthy() check with explicit assertions against the users
variable).
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/plugins/policy/src/policy-handler.tstests/regression/test/issue-2385.test.ts
fixes #2385
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests