Repository navigation
fix(sql-orm-client): don't type required to-one relations as nullable - #269
Conversation
📝 WalkthroughWalkthroughType-level logic now computes to-one include nullability by inspecting relation parent columns and foreign keys; include types reflect that. The contract schema adds profiles and articles with explicit foreign keys. Tests and an example integration test were updated to align assertions with the refined include nullability. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~40 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
@prisma-next/runtime-executor
@prisma-next/mongo-core
@prisma-next/mongo-orm
@prisma-next/mongo-runtime
@prisma-next/sql-runtime
@prisma-next/extension-paradedb
@prisma-next/extension-pgvector
@prisma-next/postgres
@prisma-next/sql-orm-client
@prisma-next/target-mongo
@prisma-next/adapter-mongo
@prisma-next/driver-mongo
@prisma-next/contract-authoring
@prisma-next/contract-ts
@prisma-next/ids
@prisma-next/psl-parser
@prisma-next/psl-printer
@prisma-next/cli
@prisma-next/emitter
@prisma-next/eslint-plugin
@prisma-next/migration-tools
@prisma-next/vite-plugin-contract-emit
@prisma-next/sql-contract
@prisma-next/sql-errors
@prisma-next/sql-operations
@prisma-next/sql-schema-ir
@prisma-next/sql-contract-psl
@prisma-next/sql-contract-ts
@prisma-next/sql-contract-emitter
@prisma-next/family-sql
@prisma-next/sql-kysely-lane
@prisma-next/sql-lane-query-builder
@prisma-next/sql-relational-core
@prisma-next/sql-builder-new
@prisma-next/sql-lane
@prisma-next/target-postgres
@prisma-next/adapter-postgres
@prisma-next/driver-postgres
@prisma-next/core-control-plane
@prisma-next/core-execution-plane
@prisma-next/config
@prisma-next/contract
@prisma-next/operations
@prisma-next/plan
@prisma-next/utils
commit: |
2befd06 to
4551264
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
examples/prisma-next-demo/test/repositories.integration.test.ts (1)
401-406: Stale TODO comment references the issue being fixed.The TODO references
TML-2120, which this PR addresses. However, this comment is about a different problem—included relations being omitted from the result type entirely—rather than the nullability issue fixed here. Consider updating or removing this comment if the issue is now closed, or updating the reference if this is tracked separately.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@examples/prisma-next-demo/test/repositories.integration.test.ts` around lines 401 - 406, The TODO comment referencing TML-2120 and IncludeRelationValue is stale and misleading relative to the current fix; update or remove it near the UserWithPosts type (and the surrounding comment block that mentions IncludeRelationValue and users) so it accurately reflects the remaining issue or is dropped if resolved—either replace the TODO with a correct issue reference and a short note about why the manual type assertion for UserWithPosts remains, or delete the comment entirely if TML-2120/IncludeRelationValue is now fixed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@examples/prisma-next-demo/test/repositories.integration.test.ts`:
- Around line 401-406: The TODO comment referencing TML-2120 and
IncludeRelationValue is stale and misleading relative to the current fix; update
or remove it near the UserWithPosts type (and the surrounding comment block that
mentions IncludeRelationValue and users) so it accurately reflects the remaining
issue or is dropped if resolved—either replace the TODO with a correct issue
reference and a short note about why the manual type assertion for UserWithPosts
remains, or delete the comment entirely if TML-2120/IncludeRelationValue is now
fixed.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 89ec6b37-b4da-498b-9c6a-25253f87d34c
📒 Files selected for processing (3)
examples/prisma-next-demo/test/repositories.integration.test.tspackages/3-extensions/sql-orm-client/src/types.tspackages/3-extensions/sql-orm-client/test/include-cardinality.test-d.ts
4551264 to
d6f4f28
Compare
|
Would it make sense to add a test that ensures that if we have a relation that is NOT backed by foreign key then relation type is still nullable, even if the key field is not? As far as I understood the types this is how they should work and this is a right thing |
As the types are currently written, that's only the case for 1:1 (and it's only used to determine which side of the relation we are on, and not because of the referential integrity) but you are right, this is a right thing to do in general. I'll update this and add the tests. |
397aa10 to
d7faa15
Compare
d7faa15 to
7a66cae
Compare
Fix the types of N:1 relationships to not mark them as nullable when the foreign key is not nullable. The situation with 1:1 is a little weird since we only represent them once in the contract (unlike 1:N which are represented both ways) and we don't seem to populate the foreignKeys in the generated contract (which is a separate bug) so the fix still doesn't work for them. I'll look into that in a follow up PR. Closes: https://linear.app/prisma-company/issue/TML-2120/includerelationvalue-type-incorrectly-marks-non-nullable-n1-relations
7a66cae to
112aabc
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/3-extensions/sql-orm-client/src/types.ts`:
- Around line 978-983: The type RelationParentCols is checking the wrong
property name; change its conditional to look for on.parentColumns (and
similarly update any checks for childCols to on.childColumns) so
RelationParentCols correctly infers the parent column tuple from Relation, and
ensure IsToOneRelationNullable and other downstream types now use that canonical
parentColumns/childColumns shape; update the type guard in RelationParentCols
and the child column check (referencing RelationParentCols and the code that
previously inspected childCols) to use parentColumns/childColumns respectively.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 83a0541b-2c00-4d96-80d1-a95ab54feb00
⛔ Files ignored due to path filters (2)
packages/3-extensions/sql-orm-client/test/fixtures/generated/contract.d.tsis excluded by!**/generated/**packages/3-extensions/sql-orm-client/test/fixtures/generated/contract.jsonis excluded by!**/generated/**
📒 Files selected for processing (4)
examples/prisma-next-demo/test/repositories.integration.test.tspackages/3-extensions/sql-orm-client/src/types.tspackages/3-extensions/sql-orm-client/test/fixtures/contract.tspackages/3-extensions/sql-orm-client/test/include-cardinality.test-d.ts
✅ Files skipped from review due to trivial changes (1)
- examples/prisma-next-demo/test/repositories.integration.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/3-extensions/sql-orm-client/test/fixtures/contract.ts
Resolve conflicts from rebasing onto origin/main: - Add FK constraints to storage tables in contract.d.ts/json fixtures (required by PR #269 to-one relation nullability logic) - Add Article model with ADR 172 structure and Profile user relation - Update RelationParentCols to RelationLocalFieldColumns: resolve localFields (field names) to column names via FieldColumnName for FK constraint matching in IsToOneRelationNullable
Resolve conflicts from rebasing onto origin/main: - Add FK constraints to storage tables in contract.d.ts/json fixtures (required by PR #269 to-one relation nullability logic) - Add Article model with ADR 172 structure and Profile user relation - Update RelationParentCols to RelationLocalFieldColumns: resolve localFields (field names) to column names via FieldColumnName for FK constraint matching in IsToOneRelationNullable
Resolve conflicts from rebasing onto origin/main: - Add FK constraints to storage tables in contract.d.ts/json fixtures (required by PR #269 to-one relation nullability logic) - Add Article model with ADR 172 structure and Profile user relation - Update RelationParentCols to RelationLocalFieldColumns: resolve localFields (field names) to column names via FieldColumnName for FK constraint matching in IsToOneRelationNullable
…#269) Fix the types of N:1 relationships to not mark them as nullable when the foreign key is not nullable. The situation with 1:1 is a little weird since we only represent them once in the contract (unlike 1:N which are represented both ways) and we don't seem to populate the foreignKeys in the generated contract (which is a separate bug) so the fix still doesn't work for them. I'll look into that in a follow up PR. Closes: https://linear.app/prisma-company/issue/TML-2120/includerelationvalue-type-incorrectly-marks-non-nullable-n1-relations <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Type handling for to-one relations now marks included rows nullable only when the schema requires it, eliminating extra non-null assertions in tests. * **New Features** * Relation nullability is inferred from declared schema and foreign-key info, improving correctness for to-one vs to-many includes. * Expanded schema coverage to include profile, invited-by, and article reviewer scenarios. * **Tests** * Strengthened and added type-level and integration tests to validate various relationship nullability and shape behaviors. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Fix the types of N:1 relationships to not mark them as nullable when the foreign key is not nullable.
The situation with 1:1 is a little weird since we only represent them once in the contract (unlike 1:N which are represented both ways) and we don't seem to populate the foreignKeys in the generated contract (which is a separate bug) so the fix still doesn't work for them. I'll look into that in a follow up PR.
Closes: https://linear.app/prisma-company/issue/TML-2120/includerelationvalue-type-incorrectly-marks-non-nullable-n1-relations
Summary by CodeRabbit
Bug Fixes
New Features
Tests