Skip to content

fix: generated prisma schema contains error when using "@@unique" with base field #1766

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

Merged
merged 2 commits into from
Oct 9, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Oct 9, 2024

Fixes #1758

ymc9 added 2 commits October 8, 2024 22:41
fix
Copy link
Contributor

coderabbitai bot commented Oct 9, 2024

📝 Walkthrough

Walkthrough

The pull request introduces enhancements to the AttributeApplicationValidator class by adding a new private method _checkUnique, which validates the uniqueness of fields in attributes, particularly concerning polymorphic models. Additionally, a regression test has been added to ensure that errors related to the use of inherited fields in unique constraints are properly handled. The changes maintain the existing validation logic while expanding the validation capabilities of the class.

Changes

File Change Summary
packages/schema/src/language-server/validator/attribute-application-validator.ts Added private method _checkUnique to validate uniqueness of fields in attributes. Updated imports.
tests/regression/tests/issue-1758.test.ts Introduced a regression test case for issue 1758 to validate handling of polymorphic model constraints.

Assessment against linked issues

Objective Addressed Explanation
Validate uniqueness of fields in polymorphic models (1758)
Ensure inherited fields cannot be used in unique constraints (1758)

Possibly related PRs


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
tests/regression/tests/issue-1758.test.ts (2)

5-27: LGTM: Assertion correctly validates the expected error.

The test assertion appropriately checks for the expected error message when attempting to use an inherited field in a @@unique constraint for a polymorphic model. This correctly validates the behavior described in issue #1758.

Consider adding a comment explaining the significance of this error message for improved clarity:

// Ensure that the system correctly identifies the invalid use of inherited fields in @@unique constraints for polymorphic models
await expect(
    loadModelWithError(
        // ... model definition ...
    )
).resolves.toContain('Cannot use fields inherited from a polymorphic base model in `@@unique`');

1-29: Great job: Effective regression test for issue #1758.

This test case effectively addresses the problem reported in issue #1758. It accurately models the scenario where a polymorphic model attempts to use an inherited field in a @@unique constraint, and correctly validates that the system identifies this as an error. The test is well-structured, concise, and focused on the specific issue at hand.

To further strengthen the test suite:

  1. Consider adding additional test cases to cover variations of this scenario, such as:
    • Multiple levels of inheritance
    • Different combinations of fields in the @@unique constraint
  2. Ensure that this test is included in the CI/CD pipeline to prevent regression in future updates.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2d42606 and 9eeed46.

📒 Files selected for processing (2)
  • packages/schema/src/language-server/validator/attribute-application-validator.ts (2 hunks)
  • tests/regression/tests/issue-1758.test.ts (1 hunks)
🧰 Additional context used
🔇 Additional comments (5)
tests/regression/tests/issue-1758.test.ts (3)

1-1: LGTM: Import statement is correct.

The import of loadModelWithError from '@zenstackhq/testtools' is appropriate for this test case.


3-4: LGTM: Test structure is well-defined.

The test suite and case are properly structured using Jest conventions. The naming clearly indicates the purpose of the test, and the use of an async test is appropriate for the asynchronous nature of the loadModelWithError function.


7-25: LGTM: Model definition accurately represents the issue scenario.

The model definition correctly implements the polymorphic relationship between Content and Store models, and includes the @@unique constraint that combines fields from both the base and extended models. This accurately represents the scenario described in issue #1758, making it an appropriate test case for the reported problem.

packages/schema/src/language-server/validator/attribute-application-validator.ts (2)

18-24: Imports added correctly for new functionality

The necessary functions have been correctly imported to support the added validation logic.


173-197: ⚠️ Potential issue

Potential conflict with PR objective: Validation prevents using inherited fields in @@unique constraints

The new _checkUnique method introduces a validation that disallows using fields inherited from a polymorphic base model in @@unique attributes (lines 187-191). However, the PR aims to allow @@unique constraints to include fields from both base and extended models, resolving issue #1758 where inherited fields were not recognized.

This validation may prevent the desired functionality and contradict the PR's purpose.

Consider revising the validation logic to allow inherited fields in @@unique constraints. You may consider removing the check that triggers the error when inherited fields are used.

Apply this diff to remove the blocking validation:

                if (!isDataModelField(item.target.ref)) {
                    accept('error', `Expecting a field reference`, { node: item });
                    return;
                }

-               if (item.target.ref.$container !== attr.$container && isDelegateModel(item.target.ref.$container)) {
-                   accept('error', `Cannot use fields inherited from a polymorphic base model in \`@@unique\``, {
-                       node: item,
-                   });
-               }

@ymc9 ymc9 merged commit a800fe6 into dev Oct 9, 2024
13 checks passed
@ymc9 ymc9 deleted the fix/issue-1758 branch October 9, 2024 06:28
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.

None yet

1 participant